In JavaScript, we can use the window.location object to force one page to load another page. We can use the location object to set the URL of the new page. There are different methods - window.location.href property, window.location.assign() and window.location.replace() methods to set the URL of a new page using the location object. We will discuss each property and method in detail in this tutorial.
Window.Position.Replace
The first method is to use the window.location.href property. This property contains information about the current URL of the page and can be used to redirect the user to a new page.
grammar
window.location.href = "new_url";
The user will be immediately redirected to the specified URL (new_url).
To redirect the user after a specified time, we can also specify the setTimout function, which allows the user to be redirected to the source URL after the time specified in the function.
setTimeout(function() { window.location.href = "https://www.tutorialspoint.com"; }, 3000);
The above example will redirect the user to the given URL (https://www.tutorialspoint.com) after 3 seconds.
Example
In this example, we define a button (Load) that will render a function forceLoad() when clicked. In the forceLoad() function, we use the window.location.href property to reload the new page - tutorialspoint home page.
<html> <body> <h2 id="Forced-Load-page-using-window-location-href-property">Forced Load page using window.location.href property</h2> <p>Click on the below button to force reload new page</p> <button onclick="forceLoad()">Load</button> <script> function forceLoad() { window.location.href = "https://www.tutorialspoint.com"; } </script> </body> </html>
Window.Position.Replace
Another way to force a redirect to another page is to use the window.location.replace property. This method helps to replace the current page in the browser history with another page, but then the user will not be able to go back to the original page.
grammar
window.location.replace("new_url");
In this syntax, we will get the same effect as the window.location.href example, but the difference here is that the user's current page will not be stored in the browser's history.
Example
In this example, we define a button (Load) that will render a function forceLoad() when clicked. The forceLoad() function renders a JavaScript method - location.replace() which replaces the current source with the source at the URL provided in the function.
Please note that once navigation occurs, it does not allow the user to navigate back to the previous page, which is possible with the location.assign() property in javascript.
<html> <body> <h2 id="Forced-Load-page-using-window-location-replace-method">Forced Load page using window.location.replace() method</h2> <p>Click below button force reload new page</p> <button onclick="forceLoad()">Load</button> <script> function forceLoad() { window.location.replace("https://www.tutorialspoint.com"); } </script> </body> </html>
Window.Position.Assignment
In this method, we use the window.location.assign method, which is used to add a new page to the browser history so that the user can return to the original page that the user browsed.
grammar
window.location.assign("new_url");
In this syntax, it will have the same effect as the window.location example, but the difference here is that the user's current page will be stored in the browser's history.
Example
In this example, we define a button (Load) that will render a function forceLoad() when clicked. The ForceLoad() function renders the JavaScript method location.assign(), which causes the window to load and display the document or page at the currently specified URL. Once navigation occurs, it also allows the user to navigate back to the previous page with the help of a property called Location.assign() by simply pressing the browser's "back" button.
<html> <body> <h2 id="Window-location-assign-method">Window.location.assign() method</h2> <button onclick="forceLoad()">Load</button> <script> function forceLoad() { location.assign("https://www.tutorialspoint.com"); } </script> </body> </html>
The difference between window.location, window.location.replace and window.location.assign
Base | window.location.href | Window.location.Assignment | Window.Position.Replace |
---|---|---|---|
definition | It gets the current URL and redirects to the new document or page specified by the URL. | It loads and displays the document or page specified in the URL. | It replaces the current page with the page specified in the URL. |
Browser History | It does not add newly loaded documents or pages to the browser history. | It saves newly loaded documents or pages in the browser history. | It will also save newly loaded documents or pages in the browser history. |
return | It allows the user to return to the previous page/document. | It also allows the user to return to the previous page. | It does not allow the user to return to the previous page. |
The main difference between the window.location.href property, window.location.replace(), and window.location.assign() methods is how they handle browser history. If we talk about location.replace method, it replaces the current URL and does not allow the user to go back to the previous page. The location.assign method will load a new document and add it to the browser's history, while allowing the user to return to a previously opened page. Finally, the window.location method is identical to location.assign in that it also adds the new document to the browser's history.
in conclusion
To summarize this article, forcing a new page to load into another page in JavaScript is a simple task and can be accomplished using multiple JavaScript methods, namely window.location.href property, window.location.replace() method Or the window .location.assign() method. All these methods help developers create dynamic and interactive web pages, which help create better user interactions and enhance user application experience.
The above is the detailed content of How to force one page to load another page in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
