AJAX传参,JS获取当前URL参数(超级实用)
[javascript] view plain copy print?
AJAX传参,JS获取当前URL参数,当我们需要获取页面的URL参数的时候,可以通过下面这段JS代码获取:
[javascript] view plain copy print?
var getParam = function(name){
var search = document.location.search;
var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
var matcher = pattern.exec(search);
var items = null;
if(null != matcher){
try{
items = decodeURIComponent(decodeURIComponent(matcher[1]));
}catch(e){
try{
items = decodeURIComponent(matcher[1]);
}catch(e){
items = matcher[1];
}
}
}
return items;
};
调用方法:
alert(getParam(‘参数名’));
URL参数示例:http://example.com?cid=123&dsf=12&ids=9384
alert(getParam(‘cid’)); ======》结果123
alert(getParam(‘ids’)); ======》结果9384
注:该方法无论任何长度的参数,中间间隔多少个参数,只要你的查找的请求参数参数名对应,就能找出!使用AJAX开发必备啊!