user.sc
user.sc

Reputation: 61

Go to random page in Javascript

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!

Upvotes: 0

Views: 158

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075079

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.

Upvotes: 5

Related Questions