JavaScript Location Object

This object is used to get information about current URL of the browser and automatically navigates the user to another page. This object is part of the window object and can be accessed using the window.location property. There is no public standard that applies to the location object, but all major browsers support it.

JavaScript Location Object Properties

The following are the properties of Location object:

  • host: It gives host name and domain name of the URL.
  • hostname: It gives domain name of the URL.
  • href: It specifies URL name.
  • pathname: It returns path name of the URL.
  • protocol: It specifies the protocol used for the URL such as http:// or https://.
  • port: It specifies port of the URL.
  • search: It is used to search portion of the URL.

JavaScript Location Object Methods

The following are the methods of Location object:

  • reload: It reloads the current document.
  • replace: It replaces current URL with new URL.
  • assign: It loads the new document.

Location Object Example

<!DOCTYPE html>
<meta charset="ISO-8859-1">
	<script type="text/javascript">
		window.location.href = "";
  • <script type=”text/javascript”> tag is used to define client side script which uses attribute type for specifying MIME type.
  • window.location.href = “”; line loads the specified URL as mentioned.

Location Object Demo

  • Save the file as JSlocation_example.html in your system.
  • Just open the file in the browser, you will see that the page is redirected to

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!