Last active

URI Parsing with Javascript

  • Download Gist
1 2 3 4 5 6 7 8 9 10
var parser = document.createElement('a');
parser.href = "";
parser.protocol; // => "http:"
parser.hostname; // => ""
parser.port; // => "3000"
parser.pathname; // => "/pathname/"; // => "?search=test"
parser.hash; // => "#hash"; // => ""

This is genius and amazingly simple. Only thing is that when I run through it, outputs "" with the port included. I was running it in Google Chrome's console…

Ah, good point. Updated gist. Try hostname.

github needs a like and a share button

what about Querystring parameters. Also you can do this with the page's URL using window.location FWIW. Did not think about dropping an anchor on the page for a general url parse though.

This is why I love JavaScript!

if you're looking for something a little more robust (that handles all the querystring stuff pretty reliably), I think this is the canonical best solution:

@getify What's unreliable about this gist that you need something "a little more robust"?


  1. parseURI splits up the query string into a hash/array so you don't have to do your own parsing of it, which is often quite error prone.
  2. parseURI works in non-DOM situations (like web workers, node.js, etc)
  3. if you set a partial URL into a link in the DOM of a browser, it will automatically canonicalize the URL to the page (relative to domain, protocol, etc). This might be what you want, or it might not. You have no choice with links, but parseURI can handle some partial URLs in a standalone way without that automatic behavior (if that's what you need, which I sometimes do).

This is awesomely simple. I use URI.js so I can get an object hash of the query string parameters but this snippet works great for times when you don't want to bring in yet another library.

Try parse jar://xxxxxx in firefox :D

FWIW, Jed Schmidt documented this technique last year during a presentation.

i rolled this trick into a nice 140-byte snippet last year.

Does anyone have a compatibility list for this method? Does it work on all browsers since IE6?

How long has this existed?

This code won't work as-is in IE6, unfortunately (though it does work in IE8, so you may be able to get by as-is, dependent on your minimum version requirements!)

In my experience, setting href on anchor elements dynamically won't work on IE6, as it only processes the href property when running through the HTML parser. I came across a technique a few years ago to do this across all modern browsers by creating a wrapper element and using innerHTML to trigger IE's parser code path:

function canonicalize(url) {
    var div = document.createElement('div');
    div.innerHTML = "<a></a>";
    div.firstChild.href = url; // Ensures that the href is properly escaped
    div.innerHTML = div.innerHTML; // Run the current innerHTML back through the parser
    return div.firstChild.href; 

:+1: this is simple and elegant!

@jayzeng: it's neither. It's not simple because it requires instantiating DOM elements unnecessarily. It's not elegant because it is unreliable. It is a hack.

I'm trolling at this point, but it's also incorrect because the protocol should be "http", not "http:".

The pathname is pathname/ in ie, while /pathname/ in Chrome.

And it will be /pathname/ if we use document.location.pathname in ie.

Where's the Like button?
/me Likes @rpflorence's comment
RegExp FTW

<a href=.. onmouseover="location=href">lol, html</a>

I added URI.js to @rpflorence's jsperf. Seems to be a decent balance between speed and ease of API consumability.

Just a note, got bitten when using this - there are some differences between browser implementations of HTMLAnchorElement@pathname

James Padolsey has fixed some of the inconsistencies mentioned and wrapped it all in a nice function. See

This approach not only parses the URL but also resolves it relative to the document/base.

Keep in mind that these definitions may not be consistent with other definitions of the same terms:

That said, yes, this rocks. :)

I've made a tiny js helper to do that, a year ago

and yet another jsperf fork -- wrapping the regex in a "nicer" return format, almost as good performance:

In Internet Explorer it appears that if using a relative URL and trying to use parser.hostname it will return a blank string.

See test case here If visited in IE you'll see Window: compared to any other browser which shows Parser:

var parser = document.createElement('a');
parser.href = '/relative/url';
console.log(parser.hostname); // IE = "", Other browsers = window.location.hostname.

You can get around this using

parser.hostname || window.location.hostname

So useful, you just made my day good sir!

This is amazing. Thank you. Is there a way to get the username and password if that was supplied? Like

parser.href = "";

parser.hostname only returns ""

@fetmar from what I can tell there isn't a way to get the username and password using this method. For that try URI.js.

That's a shame, but it doesn't work in IE9... Had to switch back to regexp. Should've read all comments, it would save me 20 minutes debugging... :)

Nice snippet, inspired by your method I built a more complete function for breaking down any string URI to an object with attributes in javascript. Including breaking down the url parameters to a set of key value pairs, accounting for multiple values of a key, and converting parameter types that are numbers back to numbers from string. You can check it out here enjoy!

Wow...this is soo amazing. One of our dev guys has written like 500 line long object to cover all the scenarios for parsing a URI using regex. I guess i will just throw that code away and will be happy to use this and normalize it for all browsers.

How about punycode?

var parser = document.createElement('a');
parser.href = "ололо.рф";
parser.hostname // => lgjclffcjaeimfimpcjkgbojfgehfopl  :(

Code below also works, that might be useful while using postMessage.


I wrote a function that returns object, which contains parameter values in current URL.

Found myself using anchor.hostname more frequently and came across this jsperf comparing the number of operations against a regex check -

I dig those gists which i can understand~~~~~~~

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.