功能:原生javascript利用正则表达式获取url的参数值
function代码如下:
function getParam(url,name){
var search = document.location.search;
var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
var matcher = pattern.exec(url);
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;
};
用法如下:
var url = "http://www.jq-school.com?name=dengjianbin&pwd=123456";
获取参数name的值如下:
var name = getParam(url,"name");
获取参数pwd的值如下:
var pwd= getParam(url,"pwd");
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jquery