Lecture

Window - window.location

The window.location object allows you to retrieve the URL of the current browser window and navigate the web page to a new URL.


Key Properties and Methods

  1. location.href: Gets or sets the full URL of the current page. (e.g., https://www.example.com/path?name=value)

  2. location.protocol: Indicates the protocol of the current URL. (e.g., http: or https:)

  3. location.hostname: Represents the domain name of the current URL. (e.g., www.example.com)

  4. location.pathname: Signifies the path portion of the current URL. (e.g., /path)

  5. location.search: Refers to the query string of the URL (the part after the ?). Query strings are used for transmitting search terms or data. (e.g., ?name=value)

  6. location.reload(): A method for refreshing the current page.


Code Examples

  1. Checking the current page URL
JavaScript
console.log(window.location.href); // e.g., "https://www.example.com/path?name=value"

  1. Navigating to a new page (redirection)
JavaScript
window.location.href = 'https://www.newwebsite.com';

  1. Checking the domain name
JavaScript
console.log(window.location.hostname); // e.g., "www.example.com"

  1. Refreshing the current page
JavaScript
window.location.reload();
Mission
0 / 1

The window.location object can be used to view or change the URL of the current page, or navigate to a new URL.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

HTML
CSS
JavaScript
Loading...