Forum Moderators: open
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
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)]);
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
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>
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>
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)] }
}
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