Search within TutorialsPark

Javascript BOM : Location Object

Learn the properties and Methods of the Location Object.

Javascript Browser Object Model : Working with Location Object

The window object property of location is used to access the URL of the document currently loaded in the window and also provides information about the general navigation functionality of the document.

Its uniqueness lies in the fact that it is a property of both window and document object, hence window.location and document.location points to the same object.

The location object can even parse the current URL into discrete segments which then can be accessed and manipulated using a series of properties.

Javascript Location Object: Properties

Property Description Example
hash The URL hash, the anchor part. "#myData"
host The servername and port number. ""
hostname Server name without the port number ""
href The full URL of the page currently loaded. ""
pathname The directory and filename of the URL. "/JavaScript/"
port The port number "8080"
protocol The protocol currently in use. "http:"
search The query string of the URL, beginning with a question mark. "?q=tutorials"

Example: Javascript Location Object Properties

Give it a TRY! » Note:location object grants access to the currently loaded URI(Uniform Resource Identifier) and its properties.

Javascript Location Object: Query String Argument

All types of data related to the location object are easily available, but the query string requires some effort, as there is no way to access it immediately like others.

In the below demo, a function is created to provide easy access to properties of location object involving search query.

Example: Javascript Location Object Query String

Give it a TRY! » Note: Supply a real query string to extract the arguments

Javascript Location Object: Methods of Location Object

Property Description Return
assign(<url>) To Navigate to the specified URL. void
replace(<url>) Removes the current URL and navigates to the specified URL void
reload() To reload the current Document. void
resolveURL(<url>) To resolve the current URL to the specified one. string

The location object provides some methods to manipulate the location object.

Javascript Location Object: assign() Method

The Location object method assign() is used to pass in an URL, follow the example below.

Example : Javascript Location Object : replace() Method

Give it a TRY! » Note: Same effects can be achieved using window.location = "URL" or location.href = "URL"

Javascript Location Object : reload() Method

The JavaScript location object method reload() is used to reload a page from the browsers cache() , rather than requesting the page from the server again.

To re-request the page from the server, you need to pass a Boolean value of true to the method.

Syntax: Javascript Location Object reload()

Example: Javascript Location Object reload()

Give it a TRY! » Note:To reload page from the server provide argument true, the default value is false.

Javascript Location Object: replace() Method

The JavaScript Location method replace() is used to provide the URL as a single argument to navigate to. However, it does not make an entry into the history stack, hence it is not possible to go the the previous page.

Example: Javascript Location Object replace() Method

Give it a TRY! » Note: The timer is used to insert a delay of 1 sec after which the page is loaded.