December 21, 2024

Using the window.location property in Javascript

I often forget how this works, so here is the different properties of window.location.
Thanks to DevGuru for the data.

To see how these work, pop open your firebug and add them to your console box and run them.

The properties:

location.hash

The hash property is a string beginning with a hash (#), that specifies an anchor name in an HTTP URL.

location.host

The host property is a string comprising of the hostname and port strings.

location.hostname

The hostname property specifies the server name, subdomain and domain name (or IP address) of a URL.

location.href

The href property is a string specifying the entire URL, and of which all other Link properties are substrings.

location.pathname

The pathname property is a string portion of a URL specifying how a particular resource can be accessed.

location.port

The port property is a string specifying the communications port that the server uses.

location.protocol

The protocol property is the string at the beginning of a URL, up to and including the first colon (:), which specifies the method of access to the URL.

location.search

The search property is a string beginning with a question mark that specifies any query information in an HTTP URL.