Is there a (native) javascript API that offers the “functionality” of window.location to custom URLs? something along the lines of
var url = new URL("http://stackoverflow.com/question?hello=world#foo");
console.log(url.hostname, url.path);
// out: "stackoverflow.com", "/question"
url.hash = "bar";
url.query = "";
console.log(url);
// out: "http://stackoverflow.com/question#bar"
I’m fairly certain there is no native API for this (for what reason ever). If someone implemented this, please share the link.
So the answer would be yes and no.
As some answer suggests, you could create an anchor element and use it to do the parsing, which is basically native code.
Live demo: http://jsfiddle.net/roberkules/H48dt/
The only difference is, that the ? is not removed after the querystring is emptied.
The “API”: