JS: Get URL (window.location)

By Xah Lee. Date: . Last updated: .

This page shows you how to get the current URL.

The window.location object contains URL info. You can use it like this:

window.location.href

The window.location object has the following properties. Their values are string.

href
Entire URL. e.g. http://example.com/4960/p?tn=1&wvr=5
. If you set window.location.href, browser will load it.
protocol
Protocol. e.g. http://, ftp://, file://
host
The host name and port number (if the URL has port number). e.g. www.example.com:8080 or empty string.
hostname
Host name. e.g. www.example.com
origin
Origin. e.g. http://www.example.com or "null"
port
Port number. e.g. 8080
pathname
Pathname, with beginning slash. e.g. /js/js_get_url.html
search
Part of URL after ?, including ?. e.g. ?topnav=1&wvr=5 [see Update URL Param]
hash
The part of url after #, including #. e.g. #hoot

JavaScript/DOM, Get/Set URL

BUY Ξ£JS JavaScript in Depth