Created
June 15, 2011 08:54
-
-
Save arnabc/1026730 to your computer and use it in GitHub Desktop.
A pretty simple URL parser for Browser
This file contains 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
// A Simple URL Parser, works in Browser only AFAIK, Please test before you use :-) | |
var Uri = ( function () { | |
var keys = ['host', 'hostname', 'protocol', 'pathname', 'search', 'hash', 'port']; | |
return { | |
// Method parses URI string and returns an object with parts name e.g. host, protocol | |
// @param {String} uri | |
parse: function ( uri ) { | |
// do nothing if not string | |
if( typeof uri != 'string' ) return; | |
var key, | |
obj = {}, | |
a = document.createElement('a'); | |
a.href = uri; | |
for( var i = 0, ln = keys.length; i < ln; i++ ) { | |
key = keys[i]; | |
if( a[ key ] ) { | |
obj[key] = a[ key ]; | |
} | |
} | |
a = null; | |
return obj; | |
} | |
} | |
} )(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment