Skip to content

Instantly share code, notes, and snippets.

@crimx
Last active August 29, 2015 14:05
Show Gist options
  • Save crimx/52a4a5066c765d4c068c to your computer and use it in GitHub Desktop.
Save crimx/52a4a5066c765d4c068c to your computer and use it in GitHub Desktop.
请编写一个 JavaScript 函数 parseQueryString,它的用途是把 URL 参数解析为一个对象,如:var url = "http://www.taobao.com/index.php?key0=0&key1=1&key2=2"; var obj = parseQueryString(url); alert(obj.key0); // 输出0
function parseQueryString(str){
var pairs = str.replace(/^.*\?/, '').split('&'),
res = {};
for (var i = pairs.length - 1; i > 0; i -= 1) {
var p = pairs[i].split('=');
res[p[0]] = p[1];
}
return res;
}
var url1 = "http://www.taobao.com/index.php?key0=0&key1=1&key2=2";
var url2 = "http://www.taobao.com/index.php?key0=0&key1=1&key2=2&key3";
console.log(parseQueryString(url1));
console.log(parseQueryString(url2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment