0

I want to create a while loop between 2 html page (Or a prompt where I can ask a question) and make so that if I give the wrong answer I get redirected to another HTML page that will remain for 5 seconds before getting back to the prompt.

I am trying the following code

<!DOCTYPE html>
<html>
<body>
    <script>
        var pass;
            pass = prompt("The corret answer is 1");
        if (pass == "1") {
            document.location.href = "https://stackoverflow.com/questions/40539097/redirect-user-to-another-html-page-if-the-condition-is-true";
        } else {
            setTimeout(function(){
              window.location.href = 'prova2.html';
         }, 5000);
        }
</script>
    <body>
</html>

The Problem is that the page "prova2.html" came out after 5 second. Instead I want that it remain visible for 5 seconds.

  • remove `setTimeout` from this page. Directly load `prova2.html` and use `setTimeout` in `prova2.html` to redirect to original/desired page. – ShivCK Apr 04 '22 at 10:05
  • Well, either add HTML to this file and show it like that, or add JS to the prova2.html.. – Wimanicesir Apr 04 '22 at 10:05
  • if you want to add logic to the second page, you can't control such logic in page1. Once the browser gets redirected to page2, it will execute the logic belonging there. So you should have a $(document).ready() handler on prova2.html that will check where you are coming from and conditionally will perform an action after a given amount of seconds. – Diego D Apr 04 '22 at 10:08

2 Answers2

0

But that's exactly what your code does: if the result of prompt is '1' it redirects to SO, otherwise you start a timer that redirects to prova2 after 5 seconds.

If you want the behaviour you're describing, you need to redirect to prova2.html immediately and in the code for prova2.html you need to set a timeout that redirects you back after 5 seconds:

in original.html (or whatever it's called for you):

<!DOCTYPE html>
<html>
  <body>
    <script>
      var pass = prompt("The corret answer is 1");
      if (pass == "1") {
        document.location.href = "https://stackoverflow.com/questions/40539097/redirect-user-to-another-html-page-if-the-condition-is-true";
      } else {
        document.location.href = 'prova2.html';
      }
    </script>
  <body>
</html>

in prova2.html:

<!DOCTYPE html>
<html>
  <body>
    <script>
      setTimeout(() => {
        document.location.href = 'original.html';
      }, 5000)
    </script>
  <body>
</html>
HumanCatfood
  • 960
  • 1
  • 7
  • 20
0

This example of prova2.html does not rely on filenames, it uses the session history of the browser:

<!DOCTYPE html>
<html>
<body>
    <script>
      document.addEventListener("DOMContentLoaded", function(event) {
        setTimeout(function(){
            history.back();
            //window.location.href = 'previous.html';
          }, 5000);
        });
    </script>
      <p>5 sec after pageload you should get redirected back</p>
    <body>
</html>

Source: https://developer.mozilla.org/en-US/docs/Web/API/History/back