Skip to content

Instantly share code, notes, and snippets.

@hellraiser75
Created June 19, 2013 10:02
Show Gist options
  • Select an option

  • Save hellraiser75/5813180 to your computer and use it in GitHub Desktop.

Select an option

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
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