JavaScript Window Location | javaScript Window

JavaScript Window Location | javaScript Window

JavaScript Window Location – In this post, we will discuss all javascript window location features and uses. How to use it.

So first of all, you need to know the first javascript window location functions/methods.

The window.location object can be used to get the current webpage (URL) and to redirect the browser to a new webpage. The window prefix is not necessary but in some cases we need it.

Some of the most important daily useful functions / methods of javascript window location are the window.location.href, the window.location.hostname, the window.location.pathname, window location.protocol, window.location.assign(), etc.

Which you can use for the following tasks like, javascript redirect on page load, how to redirect to another page in javascript on button click, how to redirect to another page in javascript with parameters, javascript redirect to relative url, how to redirect from one page to another in html on button click, javascript redirect after 5 seconds, html button onclick redirect to another page and, how to redirect to another page in html after login, etc.

JavaScript Window location Methods/Functions

  • window.location.href => get the current webpage.
  • window.location.hostname => get the webhost (domain name).
  • window.location.pathname => get filename path of the current webpage.
  • window.location.protocol => get the web protocol used (http: or https:)
  • window.location.assign => loads a new document.

1: The Window location href

The window.location.href method return the current webpage URL.

Example:

var exp = window.location.href;

document.write("Current url of browser =  "+exp);

2: Window Location Hostname

The window.location.hostname method used to which is return the name of the current pages internet host.

Example:

var exp = window.location.hostname;

document.write("hostname =  "+exp);

3: Window Location Pathname

The window.location.pathname method is used for get the pathname of the current webpage.

Example:

var exp = window.location.pathname;

document.write("Pathname =  "+exp);

4: Window Location Protocol

The window.location.protocol method is used for get the protocol of the current webpage.

Example:

var exp = window.location.protocol;

document.write("Protocol =  "+exp);

5: Window Location Assign

The window.location.assign() method loads a new document.

Example:

function newDocument() { 
   window.location.assign("https://www.tutsmake.com/")  	
}

Recommended JavaScript Posts

  1. JavaScript: String Methods/Functions
  2. JavaScript: Array Methods/Functions with Example
  3. Sorting Using JavaScript Array Sort() Method
  4. JavaScript Get-Date Methods
  5. Set Date JavaScript Methods
  6. JavaScript parseFloat: Convert String to Float Number
  7. JavaScript forEach Method By Example

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

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