JavaScript Window Location Object Full Guide

Page 1

JavaScript

Window Location Object Full 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 - https://something.com/test

Then the window.location.href will set "https://something.com/test" 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 = 'https://google.com';

If Your current page url is http://something.com then the above JavaScript code will redirect the page to https://google.com

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- google.com/login then the hostname for this page is google.com

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 - www.anything.com/blog/test.html

Then script will set www.anything.com 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 - www.itscybertech.com/p/about.html

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- https://facebook.com

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.

Ex -

window.location.assign('https://www.itscybertech.com');

This code will load http://www.itscybretch.com .

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.