Last active
March 18, 2021 11:31
-
-
Save sofish/505e3c63f08dee01e543 to your computer and use it in GitHub Desktop.
Revisions
-
sofish revised this gist
Apr 16, 2015 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,13 @@ var parser = function(url) { search = search.slice(1).split('&'); for(var i = 0, arr; i < search.length; i++) { arr = search[i].split('='); var key = arr[0], value = arr[1]; if(/\[\]$/.test(key)) { ret[key] = ret[key] || []; ret[key].push(value); } else { ret[key] = value; } } return ret; }; @@ -24,7 +30,7 @@ var parser = function(url) { } }; var url = 'http://sub.example.com:8023/home/?foo=bar&ciao=cc&arr[]=1&arr[]=2#hash'; parser(url); // 结果是? -
sofish created this gist
Oct 28, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ var parser = function(url) { var a = document.createElement('a'); a.href = url; var search = function(search) { if(!search) return {}; var ret = {}; search = search.slice(1).split('&'); for(var i = 0, arr; i < search.length; i++) { arr = search[i].split('='); ret[arr[0]] = arr[1]; } return ret; }; return { protocol: a.protocol, host: a.host, hostname: a.hostname, pathname: a.pathname, search: search(a.search), hash: a.hash } }; var url = 'http://sub.example.com:8023/home/?foo=bar&ciao=cc#hash'; parser(url); // 结果是?