Thursday, September 17, 2009


I cannot count how many times I've saved this snippet's bookmark in 3 different browsers, yet when I need it, it's NOT BEEN SAVED! ARGH! So I'm solving the damn problem once and for all!

Say you have a location of:

This corresponds to:



alert(location.href); // displays ''

alert(location.protocol); // displays 'http:'
alert(location.hostname); // displays ''
alert(; // displays ''
alert(location.port); // displays '8080'
alert(location.pathname); // displays '/some/where/deep/inside/filename.htm'
alert(; // displays '?name=martin'
alert(location.hash); // displays 'anchorname'

To get just the directory path, use:

alert(location.pathname.substring(0,location.pathname.lastIndexOf('/'))); // displays '/some/where/deep/inside'


No comments:

Post a Comment