...

View Full Version : Random Java Redirect?



devGOD
08-20-2002, 07:27 PM
Does anyone have a javascript coding that will randomly redirect a user to one of three links list?

beetle
08-20-2002, 07:59 PM
var arrLinks = ('page1.htm','page2.htm','page3.htm')

function redir() {
var rand = Math.floor(Math.random() * arrLinks.length);
top.location.href=arrLinks[rand];
}

<a href="javascript:redir()">Redirect Me!</a>

devGOD
08-20-2002, 08:28 PM
how would I make it redirect on page load? would a onload work?

<script javascript>
var arrLinks = ('page1.htm','page2.htm','page3.htm')

function redir() {
var rand = Math.floor(Math.random() * arrLinks.length);
top.location.href=arrLinks[rand];
}
</script>
<body onload="redir()">

beetle
08-20-2002, 08:32 PM
Sure! Should work with any event handler....

devGOD
08-20-2002, 09:16 PM
how come this isn't working?

<html>
<head>
<title></title>
</head>
<script language="JavaScript">
var arrLinks = ('http://www.codes.com','http://www.yahoo.com','http://www.hotmail.com')

function redir() {
var rand = Math.floor(Math.random() * arrLinks.length);
top.location.href=arrLinks[rand];
}

</script>
<body onload="redir()">

</body>
</html>

beetle
08-20-2002, 09:26 PM
Oops, sorry, in my haste I made 2 mistakes. Here

var arrLinks = new Array('page1.htm','page2.htm','page3.htm')

function redir() {
var rand = Math.ceil(Math.random() * arrLinks.length);
top.location.href=arrLinks[rand];
}

devGOD
08-20-2002, 09:51 PM
the first link intel.com doesn't seem to load. an undefined error occurs

<html>
<head>
<title></title>
</head>
<script language="JavaScript">
var arrLinks = new Array('http://www.intel.com','http://www.yahoo.com','http://www.hotmail.com')

function redir() {
var rand = Math.ceil(Math.random() * arrLinks.length);
top.location.href=arrLinks[rand];
}


</script>
<body onload="redir()">

</body>
</html>

beetle
08-20-2002, 10:17 PM
Oops again. Change Math.ceil back to Math.floor



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum