How to make a Random Page Generator
78Random Page Generator
You may be wondering what use this is but I have a game site and this button allows my users to quickly get a game without having to go looking through the entire arcade. This also makes it more likely that they will find a game they like which they may have passed over before because the screen shot in the arcade didnt interest them.
So what the button does is this:
- The user clicks it
- It randomly picks a game from the site
Sounds simple but it took me ages so hopefully this will speed it up for the next person who trys to make one.
Making the Button
Ok the first thing we need to do is to make a function which will return a randomly generated number. This is done with the following code:
function get_random()
{
var ranNum= Math.floor(Math.random()*89);
return ranNum;
}
I needed it to select from 89 pages so I have told it to use 89 numbers. These numbers will range from 0 to 88.
Next we need a function to specify what should be done for each eventuality. This is done with the following code:
function getaGame()
{
var whichGame=get_random();
var game=new Array(89)
game[0]= "http://supremegames.synthasite.com/18-wheeler-2.php";
game[1]= "http://supremegames.synthasite.com/battleships.php";
.
.
.
.
game[87]= "http://supremegames.synthasite.com/king-of-the-hill.php";
game[88]= "http://supremegames.synthasite.com/polar-jump.php";
location.href = game[whichGame];
}
The line var whichGame=get_random(); tells the function getaGame to set whichGame to the random value created by the function get_random()
The following lines tell the function to create an array with 89 possibilitys and then each possibilty is given the required link:
var game=new Array(89)
game[0]= "http://supremegames.synthasite.com/18-wheeler-2.php";
And finally location.href = game[whichGame]; tell it to open the page relating to the random number.
Next is to create the button this is done with the following code:
<BODY>
<FORM name="form1">
<center>
<INPUT TYPE="button" value="Click Here For A Random Game!" onClick="getaGame()" button style="font: bold 40px Arial; color: blue">
</center>
</FORM>
</BODY>
To create the button INPUT TYPE="button" is used.
To write a message on the button value="Click Here For A Random Game!" is used.
To tell the button what to do when it is clicked is dictated by onClick="getaGame()"
And the styling for the button is done with the following:
button style="font: bold 40px Arial; color: blue"
And The Finished Product
Ok when you put it all together you get the following code:
<HEAD>
<SCRIPT language="JavaScript">
<!--
function get_random()
{
var ranNum= Math.floor(Math.random()*89);
return ranNum;
}
function getaGame()
{
var whichGame=get_random();
var game=new Array(89)
game[0]= "http://supremegames.synthasite.com/18-wheeler-2.php";
game[1]= "http://supremegames.synthasite.com/battleships.php";
game[2]= "http://supremegames.synthasite.com/monkey-lander.php";
game[3]= "http://supremegames.synthasite.com/balance.php";
game[4]= "http://supremegames.synthasite.com/alpha-attack.php";
game[5]= "http://supremegames.synthasite.com/cable-capers-2.php";
game[6]= "http://supremegames.synthasite.com/color-burst.php";
game[7]= "http://supremegames.synthasite.com/gravity.php";
game[8]= "http://supremegames.synthasite.com/last-shot.php";
game[9]= "http://supremegames.synthasite.com/moto-madness.php";
game[10]= "http://supremegames.synthasite.com/snow-line.php";
game[11]= "http://supremegames.synthasite.com/wakeboarding-xs.php";
game[12]= "http://supremegames.synthasite.com/dune-bashing.php";
game[13]= "http://supremegames.synthasite.com/rural-racer.php";
game[14]= "http://supremegames.synthasite.com/soul-of-the-katana.php";
game[15]= "http://supremegames.synthasite.com/fowl-words.php";
game[16]= "http://supremegames.synthasite.com/18-wheeler.php";
game[17]= "http://supremegames.synthasite.com/big-truck-adventures-2.php";
game[18]= "http://supremegames.synthasite.com/surfs-up.php";
game[19]= "http://supremegames.synthasite.com/bubble-trouble.php";
game[20]= "http://supremegames.synthasite.com/pearl-diver.php";
game[21]= "http://supremegames.synthasite.com/cake-quest.php";
game[22]= "http://supremegames.synthasite.com/commando.php";
game[23]= "http://supremegames.synthasite.com/penguin-push.php";
game[24]= "http://supremegames.synthasite.com/evil-minion.php";
game[25]= "http://supremegames.synthasite.com/paintball.php";
game[26]= "http://supremegames.synthasite.com/ufo-joe.php";
game[27]= "http://supremegames.synthasite.com/gyroball.php";
game[28]= "http://supremegames.synthasite.com/mini-moto.php";
game[29]= "http://supremegames.synthasite.com/mountain-bike.php";
game[30]= "http://supremegames.synthasite.com/tu-95.php";
game[31]= "http://supremegames.synthasite.com/3-foot-ninja.php";
game[32]= "http://supremegames.synthasite.com/rigelian-hotshots.php";
game[33]= "http://supremegames.synthasite.com/bloomin-gardens.php";
game[34]= "http://supremegames.synthasite.com/magic-balls.php";
game[35]= "http://supremegames.synthasite.com/panik-in-chocoland.php";
game[36]= "http://supremegames.synthasite.com/bunch.php";
game[37]= "http://supremegames.synthasite.com/conquer-antartica.php";
game[38]= "http://supremegames.synthasite.com/deep-freeze.php";
game[39]= "http://supremegames.synthasite.com/field-goal.php";
game[40]= "http://supremegames.synthasite.com/ice-hockey.php";
game[41]= "http://supremegames.synthasite.com/mission-mars.php";
game[42]= "http://supremegames.synthasite.com/mushroom-farm-defender.php";
game[43]= "http://supremegames.synthasite.com/snake.php";
game[44]= "http://supremegames.synthasite.com/sushi-go-round.php";
game[45]= "http://supremegames.synthasite.com/super-fishing.php";
game[46]= "http://supremegames.synthasite.com/fowl-words-2.php";
game[47]= "http://supremegames.synthasite.com/the-simpson-maker.php";
game[48]= "http://supremegames.synthasite.com/smashing.php";
game[49]= "http://supremegames.synthasite.com/helicopter.php";
game[50]= "http://supremegames.synthasite.com/three-foot-ninja-2.php";
game[51]= "http://supremegames.synthasite.com/tennis-ace.php";
game[52]= "http://supremegames.synthasite.com/canon-blast.php";
game[53]= "http://supremegames.synthasite.com/red-beard.php";
game[54]= "http://supremegames.synthasite.com/boom-boom-volleyball.php";
game[55]= "http://supremegames.synthasite.com/shove-it.php";
game[56]= "http://supremegames.synthasite.com/dare-devil.php";
game[57]= "http://supremegames.synthasite.com/ice-slide.php";
game[58]= "http://supremegames.synthasite.com/mini-pool-2.php";
game[59]= "http://supremegames.synthasite.com/ping-pong.php";
game[60]= "http://supremegames.synthasite.com/the-farmer.php";
game[61]= "http://supremegames.synthasite.com/twiddlestix.php";
game[62]= "http://supremegames.synthasite.com/blox-forever.php";
game[63]= "http://supremegames.synthasite.com/crashdown.php";
game[64]= "http://supremegames.synthasite.com/lunar-command.php";
game[65]= "http://supremegames.synthasite.com/detonator.php";
game[66]= "http://supremegames.synthasite.com/galactic-warrior.php";
game[67]= "http://supremegames.synthasite.com/space-escape.php";
game[68]= "http://supremegames.synthasite.com/jungle-assassin.php";
game[69]= "http://supremegames.synthasite.com/alien-attack.php";
game[70]= "http://supremegames.synthasite.com/sim-taxi.php";
game[71]= "http://supremegames.synthasite.com/urban-sniper.php";
game[72]= "http://supremegames.synthasite.com/park-a-lot-2.php";
game[73]= "http://supremegames.synthasite.com/warrior-quest.php";
game[74]= "http://supremegames.synthasite.com/storm-rage.php";
game[75]= "http://supremegames.synthasite.com/zed.php";
game[76]= "http://supremegames.synthasite.com/overlord.php";
game[77]= "http://supremegames.synthasite.com/reel-gold.php";
game[78]= "http://supremegames.synthasite.com/gold-miner-holiday-haul.php";
game[79]= "http://supremegames.synthasite.com/alien-clones.php";
game[80]= "http://supremegames.synthasite.com/a-day-of-slacking.php";
game[81]= "http://supremegames.synthasite.com/shootin-hoops.php";
game[82]= "http://supremegames.synthasite.com/bug-on-a-wire.php";
game[83]= "http://supremegames.synthasite.com/canyon-glider.php";
game[84]= "http://supremegames.synthasite.com/cube-buster.php";
game[85]= "http://supremegames.synthasite.com/deluxe-pool.php";
game[86]= "http://supremegames.synthasite.com/gangsters-life.php";
game[87]= "http://supremegames.synthasite.com/king-of-the-hill.php";
game[88]= "http://supremegames.synthasite.com/polar-jump.php";
location.href = game[whichGame];
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name="form1">
<center>
<INPUT TYPE="button" value="Click Here For A Random Game!" onClick="getaGame()" button style="font: bold 40px Arial; color: blue">
</center>
</FORM>
</BODY>
Useful Links
Go to my site Supreme Games to see how the button works.
Here are some of resources I used while trying to make this Random Page Generator.
- Getting Random - Example of how to generate random numbers using Java script.
- Dream in Code - Example for how to get the button to open a webpage.
- Javascript Kit - Examples of how to style buttons.
- brnurosci - Examples of Button background style.
- Javascript - I found how to centre the button here.
CommentsLoading...
Useful hub! If you have a WordPress blog there's a plugin that does this too.
Hey um, that script gets blocked by some browsers, is there any other way without using a script? lol cool though
Maybe its my onload, i might be wrong lol
Sorry i figured it out, if you test from your own drive in IE it freaks out, but once You put on the web it doesnt? thats my guess of why it wont work in IE.
Hi yes I just made it to work in my html site so I tested it inside that so I never actually seen it loaded as a normal file, glad you got it working because I wouldn't have been able to help :)
Hi, This works like a charm!! Thanks so much!
Is there a way to assign it to an image? And can i have this opening the pages in a new window?
Greetings from The Netherlands
Greetings,
Is there a possibility that an array can only link to a folder (may contain 50 pages) and link to any of these pages randomly?
Basically what I want is:
Each array may link to one folder - each folder carrying few or more pages.
Hi Gzirock
This is really a great little script, and i understand by looking at the comments here, you may no tbe around much.
Is there anyway that when the link is clicked a New Window can be opened?
I messed around a little with it but for the life of me cant get new pages to open in a new window
Ronnie
Hello! Does anyone know of a random page generator that allows you to submit a prefered domain, within which the random page will be found?
For example, I'd like to be able to select a random page from within listentoamovie.com by making a randompage link, and then make a bookmark for that link, so that each time I click it I get taken to any page within listentoamovie.com.
Thanks.
works perfectly. Thanks!
Thank you very much, This was very helpful!
This has really helped! I've used it on my photo's page, video's page and everything and it works great :D thanks :)








Ktoo 3 years ago
Thank you, I've been looking for a random page generator without having to learn the script to do it myself. I also need a script to generate random breakfast/lunch/dinner menus from a list of available ones, I am going to post a request for that if you want to answer it :)