0

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!

0

1 Answer 1

5

Well, let's see. There's

  • Math.random(), which will give you a random number between 0 (inclusive) and 1 (exclusive)

  • window.location, a property to which you can assign a URL to take the browser to a new page

  • Arrays, 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.

Sign up to request clarification or add additional context in comments.

1 Comment

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.