-
-
Save andyford/5b66a1fdcf5a45800dfe to your computer and use it in GitHub Desktop.
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 characters
| /* | |
| * 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