Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shuffle an array of links... 2

Status
Not open for further replies.

soonkhoo

Programmer
Dec 8, 2000
133
MY
Hi,

I have an array of links, for example:

Link A,B,C,D

and it is at the bottomFrame. The Mainframe loads the links.

But how do I program that when the next visit to this frameset, the array of links will be at random?

Link B,D,A,C?

Oh.. the Mainframe loads ABCD links of which these individual pages has a button to go next until they completed the array of links. Thank you.

Greatly Appreciated,
Soon
 
I have a slight change of question.

How do I create a "random" links? Forget about the shuffle thingy... I've created 2 framesets of different arrangements of links.. Thanks.
 
To create them, I reckon just store the href strings in an array, then allocate them randomly to the links onLoad - of course making sure you don't get repeats.
So you could make an array like this:

var link_hrefs = new Array();
links_array[0] = new Array("someURL.asp",false);

The boolean value, say called a 'key' for future reference is used to check whether this href has been assigned to any link yet - since you will have a function to assign them like this:

go thru links array.
choose random href from link_hrefs.
if(!key){
apply href to current link.
set the key to false, so it can't be chosen again.
}
else {
take another random number;
keep trying until one is found where key is false.
}

Make sure for each link - it goes thru and finds an href for it.
 
Thanks for that tip, it helps. One more question, if I have an array in the topFrame where it loads the links in the mainFrame, how can I create a link in each of the links iin the mainFrame to follow the arrays in the topFrame? Say the array is

var quiz = new Array()

quiz[0] = new quizPage("L1.htm")
quiz[1] = new quizPage("L2.htm")
quiz[2] = new quizPage("L3.htm")

I know it could be hard coded to link to the next page.. BUT, is there a dynamic way of creating a function say

function goNext(){
var i
for (i=0; i<(quiz.length-1); i++) {
parent.content.location.href = quiz[i+1].src
}
}

and then have a layer with an image that says click here.
onClick=&quot;javascript:goNext()&quot; ??

I don't know how to write the function though... thanks.
 
I just found a script for randomizing links, i tried to customize it.. but I guess I didn't understand much here.

2 framesets... A(topFrame) & B(content)

topFrame A: includes

<html>
<head>
<script language=&quot;Javascript&quot;>
<!--
// please keep these lines on when you copy the source
// made by: Nicolas -
var currentdate = 0
var theranlink = &quot; &quot;
var core = 0

function StringArray (n) {
this.length = n;
for (var i =1; i <= n; i++) {
this = ' '

}
}

link = new StringArray(4)
link[0] = 'page1.htm'
link[1] = 'page2.htm'
link[2] = 'page3.htm'
link[3] = 'page4.htm'

var ran = 60/link.length

function ranlink() {
currentdate = new Date()
core = currentdate.getSeconds()
adcore = Math.floor(core/ran)
core = adcore
theranlink = link[core]
parent.content.location = theranlink
}

//-->
</script>
</head><body>
<a href=&quot;javascript:ranlink()&quot;>NEXT</a>
</body></html>


content B: includes

link pages...

My question is, how do I shuffle these links so that it would not have any repeats? I'm not familiar with Boolean that &quot;bangers&quot; talked about. Sorry. But &quot;bangers&quot; idea is what I need.

Thanks
soonkhoo
 
Oh this script depends on time.. what I need is when the NEXT is clicked... it starts randomizing.. so, everything got shuffled without repeats.
 
maybe this is abit better...

<script language=&quot;Javascript&quot;>
<!--

var theranlink = &quot; &quot;

function StringArray (n) {
this.length = n;
for (var i =1; i <= n; i++) {
this = ' '

}
}
link = new StringArray(4)
link[0] = 'page1.htm'
link[1] = 'page2.htm'
link[2] = 'page3.htm'
link[3] = 'page4.htm'

var ran = link.length

function ranlink() {
var totallinks = 4
adcore = Math.floor(Math.random()*ran)
theranlink = link[adcore]
parent.content.location = theranlink
}
//-->
</script>


without the time.. but still there are repeats. So... need to stop them. How?

soonkhoo
 
how about this -
once a link is displayed, you switch that link with the one at the end of the array, then decrease the length of the array by one. This way, you won't get any more repeating links, and each time you search through the array, the length will be one shorter, ergo decreasing the amount of looping you have to do.


hope this helps
leo
 
uhmmm... sounds good but i don't how to switch. :(
 
Oh...actually, how do I customize it so that the order is like these:

pages 1,2,3,4 / 2,3,1,4/ 3,2,1,4 etc.

Notice that array 4 is always constant to be the last link. Is it possible??
 
Ok... i had switched to the last array.. how do I decrease the length now?
 
you don't really decrease the length,
instead you decrease the count that you are counting to.

ex:
var arrLng = myarr.length();

for(i=0;i<arrLng;i++){
//do array swap code
arrLng--;
}

sorry about not posting more specific code, i'm a little busy here at work.

hope this helps
leo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top