Skip to content

Instantly share code, notes, and snippets.

@vinicius-stutz
Last active November 15, 2015 23:39
Show Gist options
  • Save vinicius-stutz/8f6b14829611812027af to your computer and use it in GitHub Desktop.
Save vinicius-stutz/8f6b14829611812027af to your computer and use it in GitHub Desktop.
Get URL Sections In Javascript

Get URL Sections in Javascript

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 github.com

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 https://gist.github.com/#top.

window.location.hash

This will return top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment