Last active
August 29, 2015 14:20
-
-
Save wwsun/3d5ba50fc56270272585 to your computer and use it in GitHub Desktop.
JavaScript解析当前页面URL中的查询字符串
This file contains hidden or 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
function getQueryStringArgs(){ | |
// 取得查询字符串,并去掉开头的问号 | |
var qs = (location.search.length > 0 ? location.search.substring(1) : “”), | |
// 用于保存数据对象 | |
args = {}, | |
// 取得每一项 | |
items = qs.length ? qs.split(“&”) : [], | |
item = null, | |
name = null, | |
value = null, | |
//used in for loop | |
i = 0, | |
len = items.length; | |
// 逐个将每一项添加到args对象中 | |
for (i=0; i < len; i++){ | |
item = items[i].split(“=”); | |
name = decodeURIComponent(item[0]); | |
value = decodeURIComponent(item[1]); | |
if (name.length) { | |
args[name] = value; | |
} | |
} | |
return args; | |
} | |
/** | |
* query string: ?q=javascript&num=10 | |
* | |
**/ | |
var args = getQueryStringArgs(); | |
alert(args["q"]); //javascript | |
alert(args["num"]); //10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment