WebMe
homeaboutportfolioservicescontact

WebMe - Website Design & Development

previous posts
archives
 

All resources published on this blog are free to use and redistribute. All we ask is that you keep any copyright notices and links to WebMe in your code. If you find something really useful we would appreciate you placing a link on your site to ours.

 
uk web design association Independent Web Developers Portal
This is a Carbon Neutral website Follow us on Twitter
All major credit cards accepted
Web Site Design BlogSubscribe to this Blog

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:
location.href
returns http://example.com/test/test.php?i=1&b=2#3

To get the hostname use:
location.hostname
returns example.com

To get the path of the current URL including the script use:
location.pathname
returns /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.search
returns ?i=1&b=2

To get just the anchor or hash part use:
location.hash
returns #3

Finally to get the protocol use:
location.protocol
returns http:

Labels: , , , ,

Design web site media
0 Comments

 

info@webme.co.uk

copyright About UsServicesPortfolioDemoGet QuoteContact