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

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
User avatar
KingSparta
 
 
Posts: 248
Joined: Tue Dec 08, 2020 6:00 pm
Location: Earth
Contact:

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

Post 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();">
Retired Military, Airborne, Air Assault, And Flight Wings.
Model Trains, Internet, Ham Radio
https://MyAAGrapevines.com
Fayetteville, NC, USA
Avatar By AI
User avatar
BaconFries
 
 
Posts: 5364
Joined: Thu Aug 16, 2007 7:32 pm

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

Post 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....
Joe_120
 
 
Posts: 30
Joined: Fri May 21, 2021 3:10 pm
Location: Cornwall

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

Post 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
If it wasn't for bad luck, I'd have no luck at all.
User avatar
KingSparta
 
 
Posts: 248
Joined: Tue Dec 08, 2020 6:00 pm
Location: Earth
Contact:

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

Post 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.
Retired Military, Airborne, Air Assault, And Flight Wings.
Model Trains, Internet, Ham Radio
https://MyAAGrapevines.com
Fayetteville, NC, USA
Avatar By AI
User avatar
KingSparta
 
 
Posts: 248
Joined: Tue Dec 08, 2020 6:00 pm
Location: Earth
Contact:

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

Post 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();">
Retired Military, Airborne, Air Assault, And Flight Wings.
Model Trains, Internet, Ham Radio
https://MyAAGrapevines.com
Fayetteville, NC, USA
Avatar By AI
Post Reply