Created
June 19, 2013 10:02
-
-
Save hellraiser75/5813180 to your computer and use it in GitHub Desktop.
Parses an url and returns an object with properties belonging to each part of the parsed Url
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
| function parseUrl (url) { | |
| var loc = { | |
| href: url, | |
| parts : url.replace('//','/').split('/') | |
| }; | |
| loc.protocol = parts[0]; | |
| loc.host = parts[1]; | |
| parts[1] = parts[1].split(':'); | |
| loc.hostname = parts[1][0]; | |
| loc.port = parts[1].length > 1 ? parts[1][1] : ''; | |
| parts.splice(0, 2); | |
| loc.pathname = '/' + parts.join('/'); | |
| loc.pathname = loc.pathname.split('#'); | |
| loc.hash = loc.pathname.length > 1 ? '#' + loc.pathname[1] : ''; | |
| loc.pathname = loc.pathname[0]; | |
| loc.pathname = loc.pathname.split('?'); | |
| loc.search = loc.pathname.length > 1 ? '?' + loc.pathname[1] : ''; | |
| loc.pathname = loc.pathname[0]; | |
| return loc; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment