Forum Moderators: open

Message Too Old, No Replies

Script Modification Help

Javascript

         

Fess

3:05 am on May 30, 2004 (gmt 0)

10+ Year Member



Hello,

I currently have this script:

<head>
<script language="JavaScript">

function GetRandom(start,end)
{
var range = end - start + 1;
var result = start + Math.floor(Math.random()*range);
return result;
}

function RandomLink()
{
window.location.href = rLinks[GetRandom(0,rLinks.length-1)];
}

var rLinks = new Array();

// Define your links here:
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa051501a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa052101a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa062501a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa070201a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa070901a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa071601a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa072301a.htm";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa073001a.htm";

</script>

</head>

<body>
<a href="javascript:RandomLink()">Random Article</a>
</body>

My only problem is for the random articles to open in a new window. Can someone please advise me on this?

Thank You
Fess

MichaelBluejay

6:55 am on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First of all, think carefully about whether you really want to be opening new windows. Users don't like them, they clutter the screen, and they break the Back button.

If you're sure you want to be opening new windows then replace this line:

window.location.href = rLinks[GetRandom(0,rLinks.length-1)] ;

with this line:

window.open(rLinks[GetRandom(0,rLinks.length-1)]);

Fess

2:26 pm on May 30, 2004 (gmt 0)

10+ Year Member



MichaelBluejay...

Thanks for your help.

I understand your point, but the script via new window(s) serves more as a convenience in this particular situation.

Regards
Fess

Fess

6:10 pm on May 30, 2004 (gmt 0)

10+ Year Member



MichaelBluejay,

I have 1 other questions.

This script's link will lead to javascript topics.

I'd like to have another link that leads to a different topic.

If I wanted to link to CG topics only(Maya, 3dsmax, LordOfTheRings etc..) Would I have to rename the function and paste a whole new script into the <head></head>?

Thank You
Fess

MichaelBluejay

6:49 pm on May 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Having a separate function is one way to do it, but here's another:

function getRandomLink(whichSet) {

if (whichSet==0) { ...get a JavaScript-related link.... }
else if (whichSet==1) { ...get a CG-related link... }

}

Call it like this:

<A href="#" onclick="getRandomLink(0)">Click for random JavaScript link</A>
<A href="#" onclick="getRandomLink(1)">Click for random CG link</A>

Fess

5:00 pm on May 31, 2004 (gmt 0)

10+ Year Member



MichaelBluejay....

I'll give it a shot, your suggested way seems more organized.

Thank You
Fess

Fess

9:18 pm on May 31, 2004 (gmt 0)

10+ Year Member



I have stumbled a little here.

After replacing the function (bold type), I'm left confused because if I'm required to call with this:

<A href="#" onclick="getRandomLink(0)">Click for random JavaScript link</A>
<A href="#" onclick="getRandomLink(1)">Click for random CG link</A>

then where do I put the "0" & the "1" in the //Define your links here area?

I'm not well versed on javascript. Can you please advise?

Thanks
Fess

<script language="JavaScript">

function GetRandom(start,end)
{
var range = end - start + 1;
var result = start + Math.floor(Math.random()*range);
return result;
}

function getRandomLink(whichSet) {

if (whichSet==0) { ...get a JavaScript-related link.... }
else if (whichSet==1) { ...get a CG-related link... }

}

var rLinks = new Array();

// Define your links here:
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa051501a.htm";
rLinks[rLinks.length] = "http://www.cgtalk.com/showthread.php?threadid=145910";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa062501a.htm";
rLinks[rLinks.length] = "http://www.cgtalk.com/showthread.php?s=fca2ece072cbe25878c4b4fd6325944e&threadid=146200";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa070901a.htm";
rLinks[rLinks.length] = "http://www.cgtalk.com/showthread.php?s=fca2ece072cbe25878c4b4fd6325944e&threadid=146154";
rLinks[rLinks.length] = "http://javascript.about.com/library/weekly/aa072301a.htm";
rLinks[rLinks.length] = "http://www.cgtalk.com/showthread.php?s=fca2ece072cbe25878c4b4fd6325944e&threadid=145734";

</script>

MichaelBluejay

2:10 am on Jun 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



function getRandomLink(whichSet) {

jsLinks = new Array("http://example1.com",
"http://example2.com",
"http://example3.com");

cgLinks = new Array("http:/cg1.com",
"http://cg2.com",
"http://cg3.com");

if (whichSet==0) { window.location.href = jsLinks[GetRandom(0,jsLinks.length-1)] }
else if (whichSet==1) { window.location.href = cgLinks[GetRandom(0,cgLinks.length-1)] }

}

Fess

8:55 pm on Jun 1, 2004 (gmt 0)

10+ Year Member



Michael...

this is the final scipt: (just modified it to open in new window)

<script language="JavaScript">

function GetRandom(start,end)
{
var range = end - start + 1;
var result = start + Math.floor(Math.random()*range);
return result;
}

function getRandomLink(whichSet) {
contentlinks = new Array("http://www.example.com/",
"");

trafficlinks = new Array("http://www.example.com/",
"");

if (whichSet==0) { window.open(contentlinks[GetRandom(0,contentlinks.length-1)]) }
else if (whichSet==1) { window.open(trafficlinks[GetRandom(0,trafficlinks.length-1)]) }

}

</script>

It works GREAT!

Again, thank you for your help regarding this problem..

Fess