Skip to content

Instantly share code, notes, and snippets.

@steel1990
Last active March 20, 2018 12:43
Show Gist options
  • Save steel1990/1dddd79cab01048a489b to your computer and use it in GitHub Desktop.
Save steel1990/1dddd79cab01048a489b to your computer and use it in GitHub Desktop.
/**
* 获取url参数值
* @param {string} url 需要获取参数的url
* @param {string} name 需要获取的参数名
* @return {string} 返回获取的参数名
*/
var getUrlParam = function(name, url){
if(!name){
return '';
}
url = url || location.search;
name = name.replace(/(?=[\\^$*+?.():|{}])/, '\\');
var reg = new RegExp('(?:[?&]|^)' + name + '=([^?&#]*)', 'i');
var match = url.match(reg);
return !match ? '' : match[1];
};
/**
* 获取一个 url 的所有参数组成的对象
* @param {string} url 需要处理的url
* @return {Object} 返回一个包含所有参数的 map
*/
var getUrlParams = function(url) {
url = url.replace(/#.*$/, '');
var queryArray = url.split(/[?&]/).slice(1);
var i;
args = {};
for (i = 0; i < queryArray.length; i++) {
var match = queryArray[i].match(/([^=]+)=([^=]+)/);
if (match !== null) {
args[match[1]] = decodeURIComponent(match[2]);
}
}
return args;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment