Skip to content

Instantly share code, notes, and snippets.

@andyford
Forked from jlong/uri.js
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save andyford/5b66a1fdcf5a45800dfe to your computer and use it in GitHub Desktop.

Select an option

Save andyford/5b66a1fdcf5a45800dfe to your computer and use it in GitHub Desktop.
/*
* original from: https://gist.github.com/jlong/2428561
*
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
*/
urlParser = {
p: null,
init: function (url) {
this.p = document.createElement('a');
this.p.href = url || window.location.href;
return this;
},
getPathname: function (seg) {
// get the full pathname or the nth segment (seg)
return seg ? this.p.pathname.split('/')[seg] : this.p.pathname;
}
};
// example usage:
// var urlSegment = urlParser.init().getPathname(2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment