Skip to content

Instantly share code, notes, and snippets.

@jinwyp
Forked from sofish/urlparser.js
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinwyp/04145ef4eee81b916d66 to your computer and use it in GitHub Desktop.
Save jinwyp/04145ef4eee81b916d66 to your computer and use it in GitHub Desktop.
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);
// 结果是?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment