Javascript DOM window object provides a location property object that enables you to work with different properties and methods regarding browser window. Javascript location object provides the access to the various properties and methods that can get or set the location URL, port, domain name, hostname or IP address of the current URL, location URL query string variables after “?” question mark, bookmark name separated from “#” hash mark. You can also redirect and reload the current location URL using Javascript window location.
Properties of Javascript Location Object
Following are names of the accessible properties of the Javascript Location object:
1. hash: location hash property returns the bookmark used with “#” mark.
2. host: location host property returns the host name or (IP address) and port number of the current URL.
3. hostname: location hostname property returns the hostname or IP address of the current URL.
4. href: location href property allows you to get or set the current location URL.
5. pathname: location pathname property returns the path URL except domain or hostname and query string variables.
6. port: location port property returns the port number of the current URL.
7. protocol: location protocol property returns the protocol name of the current URL such http:, https: or ftp: etc.
8. search: location search property returns the query string variables from the “?” question mark
Functions of Javascript Location Object
1. reload: location reload function refreshes the current page.
2. replace: location replace function replaces the current url with new URL passed to the location replace function.
JavaScript Window Location Object Examples:
You can see the live samples and examples of JavaScript Window Location Object from the following links:
Continue to next tutorial: Javascript Window Location Replace Function to learn how to replace the current URL of browser window.