var X2={} //my namespace:)
X2.Eval=function(code){
if(!!(window.attachEvent && !window.opera)){
//ie
execScript(code);
}else{
//not ie
window.eval(code);
}
}
function createUrlPar(){//è¿åä¸ä¸ªSringæ°ç»
var str = location.href;//
var num = str.indexOf("?");
var values = "{" ;
if(num>0){
str=str.substr(num+1);
var temp = str.split("&");
for(var i=0;i<temp.length;i++){
var tt= temp[i].split("=");
var va = tt[1] == "null"?"":tt[1];
values += '"'+tt[0]+'":"'+va+'"';
if(i!=temp.length-1){
values +=','
}
}
}
values +="}";
X2.Eval("var urlPar = "+values );
}
window.οnlοad=createUrlPar();
测试地址http://localhost:5050/eas/test.html?urlid=1&urltitle=2
<script>
var urlid=urlPar['urlid']
var urlid=urlPar['urltitle']
alert("取得参数:urlid="+urlid+",urltitle="+urltitle);
</script>
javascript封装url中参数为对象
最新推荐文章于 2020-03-18 14:14:59 发布