Getting path and location details in Javascript
A couple of quick tips for you to get path information for your page in JavaScript, assuming that the URL we are manipulating is http://example.com/test/test.php?i=1&b=2#3
To get the complete URL use:
To get the hostname use:
To get the path of the current URL including the script use:
To get the current URL excluding the script use:
Now to get passed parameters from the QueryString:
To get just the anchor or hash part use:
Finally to get the protocol use:
location.hrefreturns http://example.com/test/test.php?i=1&b=2#3
To get the hostname use:
location.hostnamereturns example.com
To get the path of the current URL including the script use:
location.pathnamereturns /test/test.php
To get the current URL excluding the script use:
location.hostname+location.pathname.substr(0, location.pathname.lastIndexOf("/"))+"/"returns example.com/test/
Now to get passed parameters from the QueryString:
location.searchreturns ?i=1&b=2
To get just the anchor or hash part use:
location.hashreturns #3
Finally to get the protocol use:
location.protocolreturns http:
Labels: html, javascript, location, source code, url

