Home > Web Front-end > JS Tutorial > How to force one page to load another page in JavaScript?

How to force one page to load another page in JavaScript?

WBOY
Release: 2023-08-28 19:53:02
forward
831 people have browsed it

JavaScript 中如何强制一个页面加载另一个页面?

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";
Copy after login

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);
Copy after login

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>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>
Copy after login

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");
Copy after login

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>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>
Copy after login

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");
Copy after login

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>Window.location.assign() method</h2>
   <button onclick="forceLoad()">Load</button>
   <script>
      function forceLoad() {
         location.assign("https://www.tutorialspoint.com");
      }
   </script>
</body>
</html>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template