I'd like to program A page with a single button, when i press it, one of the pages (from a list in Javascript code) should be randomly opened. How do I do it? Thanks a lot!
1 Answer
Well, let's see. There's
Math.random(), which will give you a random number between0(inclusive) and1(exclusive)window.location, a property to which you can assign a URL to take the browser to a new pageArrays, which can contain strings, which can be URLs
Put them together, and you have the ability to pick a URL at random (from the array, using Math.random) and tell the browser to go there.
1 Comment
Michael Cordingley
To build on T.J.'s answer, there is one potentially tricky bit remaining: converting a random decimal (from
Math.random()) to a random index. You can get that with min + Math.floor(Math.random() * (max - min + 1))