The window.location
property in JavaScript is a reference to the current URL of the document that is being displayed in the browser window. This property is a read-only property, so you cannot directly change the URL of the window using this property. However, you can use it to redirect the user to a different URL by assigning a string value representing the new URL to the window.location.href
property.
Here is an example of how you can use the window.location
property to redirect the user to a different URL:
// Redirect the user to https://www.example.com
window.location.href = "https://www.example.com";
// Get the protocol of the current URL (e.g. "https:")
var protocol = window.location.protocol;
// Get the hostname of the current URL (e.g. "www.example.com")
var hostname = window.location.hostname;
// Get the pathname of the current URL (e.g. "/search")
var pathname = window.location.pathname;
// Get the query string of the current URL (e.g. "?q=javascript+window+location")
var queryString = window.location.search;
It’s important to note that the window.location
property is a part of the Window object, which represents the current browser window. This means that you must use it in the context of a web page that is running in a browser window. You cannot use it in a standalone JavaScript file or in a Node.js environment.
Leave a Reply