Skip to content

JavaScript Window location redirect

  • by

In JavaScript, you can use the window.location object to manipulate the current URL of the browser. One common use case is to redirect the user to a different page. To perform a redirect, you can use the window.location.href property to set the new URL.

Here’s an example of how you can redirect the user to a new page using JavaScript:

// Redirect to a new page
window.location.href = "https://www.example.com";

JavaScript Window location redirect example

Simple example code demonstrating a JavaScript redirect using the window.location object:

<!DOCTYPE html>
<html>
<head>
  <title>Redirect Example</title>
</head>
<body>

  <h1>Welcome to the Redirect Example</h1>

  <script>
    // Redirect to a new page after 3 seconds
    setTimeout(function() {
      window.location.href = "https://eyehunts.com";
    }, 3000); // 3 seconds
  </script>

</body>
</html>

Output:

JavaScript Window location redirect

In the above example, the page displays a heading “Welcome to the Redirect Example.” After a delay of 3 seconds (specified in milliseconds), the JavaScript code inside the <script> tags triggers a redirect using window.location.href.

Do comment if you have any doubts or suggestions on this JS window object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading