JavaScript Window Location Object Full Guide

JavaScript Window Location Object Full Guide

Java Script guide You will learn that how you can use window location object of JavaScript. In this post you will get the all information about window location object and how can you use it in different ways

What is the use of window.location


  •  First we can use it for getting the value of current web page address or URL
  • Second For redirecting the current page to new page.

Some Types Of Window location

There are 5 basic types o window.location. All 5 types will do different work.
  1. window.location.href
  2. window.location.hostname
  3. window.location.pathname
  4. window.location.assign()
  5. window.loacation.protocol

Window Location Href ( window.location.href )

The window.location.href gives us the current web page address or URL and it can redirect the current page also.
Get The Current Page URL
var OP = window.location.href;
if your current page url is – 
then the window.location.href will set “”  the value of variable OP
Redirect The Current Page 
For redirecting the current page URL we will manually set the value of window.location.href.
window.location.href = '';
If Your current page url is then the above JavaScript code will redirect the page to

Window Location Hostname ( window.location.hostname )

window.location.hostname gives us the name of domain of current page or Hostname.
Hostname means where the files are hosted.
if you open a URL ex- then the hostname for this page is
Code – 
var op = window.location.hostname;
This javascript code will return the current page hostname as a value of variable op
If this code is written in this page  –
then script will set as the value of variable op 

Window Location Pathname (window.location.pathname)

window.location.pathname gives us the path address of the current page.
If You are in this page ex –
then the path name for the above url is /p/about.html
var op = window.location.pathname;
the above JavaScript code will set the current page pathname as the value of variable op

Window Location Protocol (window.location.protocol)

The window.location.protocol gives us the web protocol of the current URL
if you are visiting a site ex-
then the webprotocol for the above URL is https:

var op = window.location.protocol;

the above JavaScript code will return the webprotocol of the current page.

Window Location Assign (window.location.assign)

The window.location.assign() simply load a new document or new URL.


Leave a Reply

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