MDN and w3schools.">

Window.Location Property | Wendy's Corner

Window.Location Property

  |   Source

window.location is a read-only property, but you can also sign a string to it. It has some commenly used properties:


You can add anchor to URL, using a # character. For example:

Use the example above, window.console.log(location.hash) => #blog_1. Returned value include #.

When you set a hash, do not include #.


Return entire URL. When you assign it, you can assign whole URL, relative URL, anchor only or protocal, based on your need. Following examples all work well: location.href="#blog_1" location.href="mailto:[email protected]" location.href="test.html" location.href=ftp//


Not supported by IE

Return protolcol.hostname and port number of a URL. An example is httpw://

Note If the port number is not specified in URL, some browsers will not display port number.

This is a read-only property, so you can not set it.

You can use this property to get/set hostname and port of URL. Normally should return "".

Note: If the port number is not specified in URL, some browsers will not display port number.

You can use = "hostname:port" to set host.

location.hostname location.port location.protocal

Similar as above, these properties returns hostname/port/protocal of URL. Normally should return "".

You can do get/set for all of these properties.

If a port is not specified in URL or is scheme's default port,such as 80 or 443,some browser return 0 or nothing.


Return directoy path of a URL. For example: `/WEB-INF/Views/Test.jsp

You can do get/set for this property.

Return the querying string part of a URL, which is normally the part ? and string after it in URL.

You can do get/set for it. For example, a possible result can be ?id=111.


Load a new document.


Same as location.assign(), but you cannot use "back" button to go back to previous page.

You can combine it with hash. For example: location.replace("" + location.pathname)


Same as the "reload" button in browser, reloading current document.

Use location.reload(true) to force reloading document.

Comments powered by Disqus