W3Schools JavaScript Window Location Href Guide: Master Page Redirects

Imagine you're building a dynamic web application with JavaScript. At some point, you'll likely want to navigate users to another page or resource. That's where the `window.location.href` property comes in. Let's dive into this powerful JavaScript tool and explore how to navigate with it, and how it integrates with the classic `` tag's `href` attribute.

Javascript Window.print()
Javascript Window.print()
the web programming poster is shown
the web programming poster is shown

The `window.location.href` can be used to navigate to a new page or refresh the current one, based on the value you pass to it. Here's how you can use it:

the csss3 class sheet is shown in orange and blue, with black letters on it
the csss3 class sheet is shown in orange and blue, with black letters on it

1. **Redirecting to a new page:** You can redirect the user to a new page by setting `window.location.href` to the URL of the page you want to navigate to. For example, to redirect to a 'contact' page:

window.location.href = 'contact.html';

JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡

Absolute vs Relative URLs

Absolute URLs start with a protocol (like 'http:' or 'https:'), while relative URLs do not. Using `window.location.href` without a protocol assumes the URL is relative to the current page. Here's an example:

window.location.href = 'https://www.example.com'; // absolute URL window.location.href = 'path/to/page'; // relative URL

an image of a computer screen with headphones on it and another screenshot of the web page
an image of a computer screen with headphones on it and another screenshot of the web page

lleno>Navigate and Replace the Current Entry in History

By default, changing `window.location.href` adds a new entry to the browser history, allowing the user to navigate back and forth using the browser's back and forward buttons. However, you can pass the `replace` option as `true` to replace the current entry in history, making the 'back' button not go back to the previous page:

window.location.href = 'new-page.html'; window.location.href = 'new-page.html', { replace: true };

the web programming poster is shown
the web programming poster is shown
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript: Map Methods
JavaScript: Map Methods
tg: webguild
tg: webguild
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
an image of a computer screen with some type of text and pictures on the screen
an image of a computer screen with some type of text and pictures on the screen
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Works in the Browser
JavaScript Works in the Browser
Monstro Galáctico
Monstro Galáctico
Javascript Cheet cheet
Javascript Cheet cheet
Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
JavaScript Fundamental
JavaScript Fundamental
Javascript String Methods
Javascript String Methods
.NET everywhere apparently also means Windows 3.11 and DOS
.NET everywhere apparently also means Windows 3.11 and DOS
Best Way to Learn JavaScript in 2024 (Free and Paid Tutorials)
Best Way to Learn JavaScript in 2024 (Free and Paid Tutorials)
an image of some type of webpage with dark colors and blue text on it
an image of some type of webpage with dark colors and blue text on it
sunset evening using html css javascript
sunset evening using html css javascript
the back side of a computer screen with an image of a black and purple background
the back side of a computer screen with an image of a black and purple background
an image of a computer screen with the text'document object model'on it
an image of a computer screen with the text'document object model'on it
Javascript tutorials
Javascript tutorials

Related Articles

Javascript Classes Example W3schools Javascript Operators W3schools Javascript Do While Loop Undefined Javascript Function Data Validation In Javascript W3schools Vanilla Js Examples Upload File Javascript Example Dynamic Html With Javascript W3schools Calculator Javascript Example Upload Image Html Javascript