#webdevelopment
#redirect webpage
#javascript
#coding
#programming

How to redirect to another webpage in Javascript

Anonymous

AnonymousJan 04, 2024

In this post, we will learn How to redirect to another webpage in Javascript.

Redirect a Webpage

// HTTP redirect
window.location.href = "http://www.techstackkit.com";

// HTTPS redirect
window.location.replace("http://www.techstackkit.com");

Example:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Redirect Example</title>
</head>

<body>
  <button id="redirectButton">Click me to redirect!</button>
  <script>
    const redirectButton = document.getElementById('redirectButton');
        redirectButton.addEventListener('click', function () {
            window.location.href = 'https:techstackkit.com';
        });
  </script>

</body>

</html>

Ouput:

Happy Coding! ❤️