The location is an object that contains information about the URL of the currently loaded page.

For example, location.href, is the full URL and location.hostname is only the domain.

An example to view of properties of the location object for an url.

Consider a URL like this:
for (var i in location) {
console.log(i + ' = "' + location[i] + '"');

href = ""
hash = "#results"
host = ""
hostname = ""
pathname = "/search"
port = "8080"
protocol = "http:"
search = "?v=javascript"

There are also three methods that location provides—reload(), assign() and replace().

There are different ways to navigate to another page using location object properties and methods.

window.location.href = ''
location.href = ''
location = ''

replace() is almost the same as assign(). The difference is that it doesn't create an entry in the browser's history list.

To reload a page you can use: location.reload()

You can use location.href to point it to itself, like so.
window.location.href = window.location.href
Or simply:
location = location

Location Object Properties

hashSets or returns the anchor part (#) of a URL
hostSets or returns the hostname and port number of a URL
hostnameSets or returns the hostname of a URL
hrefSets or returns the entire URL
originReturns the protocol, hostname and port number of a URL
pathnameSets or returns the path name of a URL
portSets or returns the port number of a URL
protocolSets or returns the protocol of a URL
searchSets or returns the querystring part of a URL

Location Object Methods

assign()Loads a new document
reload()Reloads the current document
replace()Replaces the current document with a new one

Related Tutorial
2 window
Follow Us # # # # #