js获取url中参数的值

function GetQueryString(name) {  
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
    var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配
    var context = "";  
        if (r != null){
            context = r[2];  
        }       
        reg = null;  
        r = null;  
        return context == null || context == "" || context == "undefined" ? "" : context;  
    }
alert(GetQueryString("id")); //使用调函数即可(如果参数值为id的话)
var data = GetQueryString("id");
//如果是汉字并出现乱码的情况
data = decodeURIComponent(data);

你可能感兴趣的:(js获取url中参数的值)