Home

window.location

Keywords: host domain, window.location.pathname, window.location.hostname, window.location

Methods of window.location

Method Name Example Explanation
assign window.location.assign("http://TundraBlue.com"); Navigates to a new page and creates a browser history item.
replace window.location.replace("http://TundraBlue.com"); Navigates to a new page and replaces the current page in browser history.
reload window.location.reload("http://TundraBlue.com"); Reloads the current location from the server no browser history item is created.

Properties of window.location

Those familiar with javascript may find this to be the easiest, quickest, cleanest example and explanation:

window.location.href is equal to the entire url as seen in the browsers url/search bar. The script:

            function LocationTest()
            {
                var loc = window.location;
                alert(loc.href);
                alert(loc.protocol + "//" + loc.host + loc.pathname + loc.search + loc.hash);
                alert(loc.href == loc.protocol + "//" + loc.host + loc.pathname + loc.search + loc.hash);
            }

Remember: What you see is what you get - what you see in your url bar is what the window.location properties will return.

Here is a complete break-down:

Property Syntax Example (all statements are true) Explanation
protocol window.location.protocol Given url(https://www.TundraBlue.com),
window.location.protocol == "https:"
Returns the protocol of a url. protocol usually is "http:" or "https:".
hostname window.location.hostname Given url(https://www.TundraBlue.com/index.html),
window.location.hostname == "www.TundraBlue.com"
Returns the host domain of a url. hostname will include any subdomains, if present, but not port numbers.
port window.location.port Given url(https://www.TundraBlue.com:80/index.html),
window.location.port == "80"
Returns the port number of a url, if it exists. port will return null if none is included.
host window.location.host Given url(https://www.TundraBlue.com:80/index.html),
window.location.host == "www.TundraBlue.com:80"
Returns the host of a url including all subdomains and port. host is essentially the same as hostname and port combined. Making the following statement true:
window.location.host == window.location.hostname+":"+window.location.port
pathname window.location.pathname Given url(https://www.TundraBlue.com/Directory/index.html),
window.location.pathname == "/Directory/index.html"
Returns the pathname of a url, if it exists. For example "https://TundraBlue.com/" only returns "/", even though "/" may represent "/index.html".
search window.location.search Given url(https://www.TundraBlue.com/index.html?SearchParameter=true#HashID),
window.location.search == "?SearchParameter=true"
Returns the parameters of a url (search), if it exists. Includes "?", when returned.
hash window.location.hash Given url(https://www.TundraBlue.com/index.html?SearchParameter=true#HashID),
window.location.hash == "#HashID"
Returns on-page navigation of a url (hash), if it exists. Includes "#", when returned.