Page 1 of 1

Pulls up a random page, it works but is there a better way?

Posted: Fri May 06, 2022 8:41 am
by KingSparta
Pulls up a random page, it works but is there a better way?

http://www.MyAAGrapevines.com

Code: Select all

<script type="text/javascript">
<!--
// Create an array of the links to choose from:
var links = new Array();
links[0] = "index0.html";
links[1] = "index1.html";
links[2] = "index2.html";
links[3] = "index3.html";
links[4] = "index4.html";
links[5] = "index5.html";
links[6] = "index6.html";
links[7] = "index7.html";
links[8] = "index8.html";
links[9] = "index9.html";
links[10] = "index10.html";
links[11] = "index11.html";
links[12] = "index12.html";
links[13] = "index13.html";
links[14] = "index14.html";
links[15] = "index15.html";
links[16] = "index16.html";
links[17] = "index17.html";
links[18] = "index18.html";
links[19] = "index19.html";
links[20] = "index20.html";
links[21] = "index21.html";
links[22] = "index22.html";
links[23] = "index23.html";
links[24] = "index24.html";
links[25] = "index25.html";
links[26] = "index26.html";
links[27] = "index27.html";
links[28] = "index28.html";
links[29] = "index29.html";
links[30] = "index30.html";
links[31] = "index31.html";
function openLink() {
  // Chooses a random link:
  var i = Math.floor(Math.random() * links.length);
  // Directs the browser to the chosen target:
  parent.location = links[i];
  return false;
}
//-->
</script>

Code: Select all

<body onload="openLink();">

Re: Pulls up a random page, it works but is there a better way?

Posted: Fri May 06, 2022 10:32 am
by BaconFries
What is it that is being asked? how to make work? Or is it being offered as Tip, Trick from you to others to use....

Re: Pulls up a random page, it works but is there a better way?

Posted: Sun May 08, 2022 5:39 am
by Joe_120
The code shown is simple but to the point and it works. If you want to try something without using an array see this link https://stackoverflow.com/questions/227 ... hout-array

Joe

Re: Pulls up a random page, it works but is there a better way?

Posted: Sun May 08, 2022 10:48 am
by KingSparta
thanks, just was not sure I was using the best way to do it.

I think I found a way to cut the array down a bit.

Re: Pulls up a random page, it works but is there a better way?

Posted: Tue May 10, 2022 9:01 am
by KingSparta
this version only stores the number in the array to choose from
and puts the link together later, (saves array\code space).

Code: Select all

<script type="text/javascript">
<!--
// Create an array:
var links = new Array();
links[0] = "00";
links[1] = "01";
links[2] = "02";
links[3] = "03";
links[4] = "04";
links[5] = "05";
links[6] = "06";
links[7] = "07";
links[8] = "08";
links[9] = "09";

function openLink() {
  // Chooses a random link:
  var i = Math.floor(Math.random() * links.length);
  // Directs the browser to the chosen target:
  parent.location = "index" + links[i] + ".html";
  return false;
}
//-->
</script>

Code: Select all

<body onload="openLink();">