Skip to content

Instantly share code, notes, and snippets.

@stemar
Last active August 29, 2015 14:16
Show Gist options
  • Save stemar/95fe2cd68219cab65843 to your computer and use it in GitHub Desktop.
Save stemar/95fe2cd68219cab65843 to your computer and use it in GitHub Desktop.
Extend JavaScript window.location with 2 properties
window.location.href returns the URL of the current page
http://www.example.org:8888/foo/bar/index.html?q=baz&w=io#bang
window.location.origin returns the protocol, hostname and port number of a URL
http://www.example.org:8888
window.location.protocol returns the web protocol used (http:// or https://)
http:
window.location.hostname returns the domain name of the web host
www.example.org
window.location.port returns the port
8888
window.location.pathname returns the path and filename of the current page
/foo/bar/index.html
window.location.search returns the querystring part of a URL
?q=baz&w=io
window.location.hash returns the anchor part of a URL
#bang
Add the path property (without the filename):
window.location.path = window.location.pathname.replace(/[^\/]*$/, '');
/foo/bar/
Add the filename property:
window.location.filename = window.location.pathname.match(/[^\/]*$/)[0];
index.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment