javascript获取url参数的方式

 方式一:
推荐使用此方式:
url链接为:newsDetail.html?id=8a8080e35f90d9fd015f90dac7750001&modelId=123456
var URLParams = new Array();  
var params = document.location.search.substr(1).split('&');   
for (i=0; i < params.length ; i++){  
    var aParam = params[i].split('=');   
    URLParams[aParam[0]] = aParam[1];   
}  
var nodeId=URLParams["id"];         //节点的id  
var modelId=URLParams["modelId"];   //模型的modelId

 

方式二:


 

此方式不推荐,匹配不一定完美,比如一些特殊字符
url链接为:http://127.0.0.1:8080/opera/index.html?token=OGE4MDgwZTM1ZjhmODQ1ODAxNWY4Zjg0NThhODAwMDA~
var url = location.search; var reg = /([^\=\?|\&]+)\=([^\=\&\#]+)/g; var parameters = url.match(reg); var token = null; url.replace(reg,function($1,$2,$3){   // 获取token
token = $3; });

 

方式二:详细例子
var url = "www.zhangyunling.com?aada=adaa&adad=adasd&sdfs=asdad#did",
reg = /([^\=\?|\&]+)\=([^\=\&\#]+)/g;
var aa = url.match(reg);
console.log(url);
console.log(aa);

url.replace(reg,function($1,$2,$3){
    console.log($2+"="+$3);
}); 

 

posted @ 2017-11-07 09:57  飘絮不飘  阅读(2906)  评论(0编辑  收藏  举报