Creating a new Location object in javascript

Well, you could use an anchor element to extract the url parts, for example:

var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
var protocol = url.protocol;
var hash = url.hash;

alert('protocol: ' + protocol);
alert('hash: ' + hash);
​

It works on all modern browsers and even on IE 5.5+.

Check an example here.

Leave a Comment