Skip to content

How do redirect to another webpage?

  • by

To redirect to another webpage, you can use the window.location object in JavaScript. Here’s an example of how to do this:

// Redirect to another webpage
window.location.href = "https://www.example.com";
// OR
location.href = 'newPage.html';

When you set this property to a new URL, the browser will automatically redirect to the specified webpage. You can also use the window.location.replace() method to redirect to a new webpage.

// Redirect to another webpage
window.location.replace("https://www.example.com");

Redirect to another webpage example

Simple example code.

<!DOCTYPE html>
<html>
<head>
    <title>Redirect to Another Webpage</title>
</head>
<body>

    <h1>Welcome to my webpage!</h1>

    <p>Click the button below to redirect to another webpage:</p>

    <button onclick="redirectToAnotherWebpage()">Go to EyeHunts.com</button>

    <script>
        // Function to redirect to another webpage
        function redirectToAnotherWebpage() {
            window.location.href = "https://eyehunts.com";
        }
    </script>

</body>
</html>

Output:

How do redirect to another webpage?

To trigger the redirect using jQuery, you can attach an event listener to an element on the page, such as a button or a link.

<button id="myButton">Go to Example.com</button>

<script>
// Attach an event listener to the button
$("#myButton").click(function() {
    // Redirect to another webpage
    window.location.href = "https://www.example.com";
});
</script>

Do comment if you have any doubts or suggestions on this webpage 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

Your email address will not be published. Required fields are marked *