Skip to content

Instantly share code, notes, and snippets.

@arnabc
Created June 15, 2011 08:54
Show Gist options
  • Save arnabc/1026730 to your computer and use it in GitHub Desktop.
Save arnabc/1026730 to your computer and use it in GitHub Desktop.
A pretty simple URL parser for Browser
// 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