window.location

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:

http://search.example.com:8080/search?v=javascript#results
snippet
for (var i in location) {
    console.log(i + ' = "' + location[i] + '"');
}
Output
href = "http://search.example.com:8080/search?v=javascript#results" hash = "#results" host = "search.example.com:8080" hostname = "search.example.com" 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.

Redirection
window.location.href = 'http://www.packtpub.com'
location.href = 'http://www.packtpub.com'
location = 'http://www.packtpub.com'
location.assign('http://www.packtpub.com')
location.replace('http://www.yahoo.com')

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

Reload

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
PropertyDescription
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
MethodDescription
assign()Loads a new document
reload()Reloads the current document
replace()Replaces the current document with a new one
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +