-
-
Save qknow-w/45e3f08c14f924e3fa82a4e954af50bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 获取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