Skip to content

Instantly share code, notes, and snippets.

@potch
Created February 1, 2013 22:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potch/4694758 to your computer and use it in GitHub Desktop.
Save potch/4694758 to your computer and use it in GitHub Desktop.
Hey everyone we solved URL parsing in JavaScript!
var urlparse = (function() {
var a = document.createElement('a');
// urlparse('https://example.com:8080/bar.html?baz#yay')
return function(url) {
a.href = url;
return {
hash: a.hash, // '#yay'
host: a.host, // 'example.com:8080'
hostname: a.hostname, // 'example.com'
href: a.href, // 'https://example.com:8080/'
pathname: a.pathname, // '/bar.html'
port: a.port, // '8080'
protocol: a.protocol, // 'https:'
search: a.search // '?baz'
};
};
})();
@tofumatt
Copy link

tofumatt commented Feb 1, 2013

Monkey-patch it onto String!

@potch
Copy link
Author

potch commented Feb 1, 2013

@tofumatt hiss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment