This comment has been minimized.

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.

parseURI works in non-DOM situations (like web workers, node.js, etc)

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 comment has been minimized.

This comment has been minimized.

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:

This comment has been minimized.

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 https://gist.github.com/aymanfarhat/5347921 enjoy!

This comment has been minimized.

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.

This comment has been minimized.

This comment has been minimized.

I know when I encounter some unknown data, my first reaction is to execute it! Genius!! Amazingly simple remote code execution via "javascript:payload"!! No user interaction required!!! Javascript is so cool!!!

This comment has been minimized.

edited

It's better to use new URL(...) when it's available. Only if it's not available (in older browsers, for example) does it make sense to use something else. In that case, the approach @jlong shows here is excellent.

This comment has been minimized.

This comment has been minimized.

Well, this great solution works only with 100% consistent URL strings, where all URL parts exists. Try example.com without the protocol:

go to any https:// website, eg. https://microsoft.com

open console

write the code:

var parser = document.createElement('a');
parser.href = "example.com"; // for example, this is user entered URL in some form's input field (`Your home page` field or something) and you trying to parse it with the method above in this topic
(You see the trick? The anchor element you've created is not going to the `example.com`, it is going on the current site's `example.com` section page — `https://microsoft.com/example.com`)

now output the protocol:

console.log(parser.protocol);

it will output https: — because you have created anchor <a> element on https://microsoft.com website