-1

I am building a quiz in a way that it does not get boring, there should always be new pages with questions. (I made the individual questions and the pages). But can you somehow program that if you have pressed a button that then a random HTML page comes. (I have the files 1.html 2.html 3.html) Can I program it by pressing a button that then either 1 2 or 3 comes? I put it in a difficult phrase, I hope you understand it anyway.

Btw. if you could show me code examples it would be nice!

Ali Heikal
  • 3,324
  • 2
  • 14
  • 23
troloxi
  • 7
  • 1
  • Yes, it is possible. Do you have any code snippets that you've tried? – King11 Jan 10 '20 at 21:55
  • The pages 1.html 2.html and 3.html and the site with the button but not the JS Code because i dont really understand this. – troloxi Jan 10 '20 at 21:57

1 Answers1

1

in the HTML code should be something like this:

<button onclick="randomQuestion()">Random question</button>

and before this button, you need to use script tag:

<script>
    function randomQuestion(){
        var page = Math.floor(Math.random() * 3) + 1; // gets random number between 1 and 3
        window.location.href = page + ".html"; // go to that link
    }
</script>