Skip to content Skip to sidebar Skip to footer

On Click Goes To Next Page - JavaScript

Hello I have one confusion in my head. I'm trying to make a logic in JS when some one click on BUTTON then automatically lead to next page after 1s. I tried to use onclick functi

Solution 1:

To go to a different page after 1 second you'll want to use a combination of setTimeout() and window.location and an onclick event on your button.

I am assuming you want to use the button next to make this happen.

First thing create a second html test page.

<html>

<body>
  <h1>Page 2</h1>
</body>

</html>

and in the code you shared, after the closing </div> tag for id next add the following:

<!DOCTYPE html>
<html>

<head>
  <title>!!!AAA!!!</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div id="btn">
    <div id="text">Button</div>
  </div>
  <div id="nextPage">
    <button id="next" onclick="switchLocation(0)">next</button>
  </div>
  <script>
    function switchLocation(counter) {
      if (counter == 0) {
        counter = 1;
        setTimeout(function() {
          switchLocation(counter);
        }, 1000);
      } else {
        window.location = "page2.html";
      }
    }
  </script>
</body>

</html>

I edited your code slightly. Removed the extra < on the first line and changed the "next" div to a button.


Post a Comment for "On Click Goes To Next Page - JavaScript"