in Javascript

Get Url Sections In Javascript

HTTP Even though most of the time to get information from the page URL you will use the server side programming language, you can actually use Javascript to get the different sections that make up the URL. ## Get Protocol

To get the URL protocol use the following snippet.


window.location.protocol

Returns http / https

Get Hostname

To get the URL host name use the following snippet.


window.location.host

Returns paulund.co.uk

Get Pathname

To get the URL pathname of the file use the following snippet.


window.location.pathname

Returns index.html

Get URL Hash Value

If a URL has a # target selected this snippet will return the value. For the URL of http://www.paulund.co.uk#top.


window.location.hash

This will return top.

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email