PDA

View Full Version : Opening a link every randomly x seconds.



lisar
Dec 30th, 2009, 04:26 PM
I am new to javascript and am trying to make a script that would open a link every xx randomly seconds.

So far I have come up with this after searching:



<script>
function reloadMe() {
window.location.reload();
}
function getInterval(){
var lowerBound = 30;
var upperBound = 60;

var randNum = Math.floor((upperBound-lowerBound+1)*Math.random()+lowerBound) * 1000;
return randNum;
}
var interval = getInterval();
var srcInterval = setInterval("reloadMe()",interval);
</script>

This works on refreashing a page every xx seconds between 30 and 60 seconds. But it does not open a link.


Is it possible to make a script that opens a link every xx seconds at random. It would be even greater if it can exclude certian times of the day, or can that be done with a cron job?

Philip M
Dec 30th, 2009, 05:01 PM
This works on refreashing a page every xx seconds between 30 and 60 seconds. But it does not open a link.


Is it possible to make a script that opens a link every xx seconds at random. It would be even greater if it can exclude certian times of the day, or can that be done with a cron job?


Sounds absolutely frightful! Why on earth do you want to inflict this on your visitors? For security reasons you cannot open a link and go to a new page without the user's consent. In other words, you cannot auto-click.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

lisar
Dec 30th, 2009, 05:34 PM
I absolutly would not want to do this with my visitors. The only person that is going to see the open links is going to be me.

Old Pedant
Dec 31st, 2009, 12:47 AM
Once again this forum swallowed a post by me!

A long one, too!

Okay, I'll answer again...shorter this time.

You would have to do this to a popup window. And the same popup window each time.

If you tried to do it into a <iframe>, many sites would break out of the iframe, destroying your page and stopping the cycle.

So:


<html>
<head>
<script type="text/javascript">
var sites = [
"http://www.cnn.com",
"http://www.espn.com",
"http://www.codingforums.com"
];

var currentSite = 0;

function nextSite( )
{
window.open( sites[currentSite], "SITES" );
document.getElementById("SHOWING").innerHTML = sites[currentSite];
currentSite = ( currentSite + 1 ) % sites.length;
}
</script>
</head>
<body onload="nextSite( )">
<h2>Now showing site <span id="SHOWING"></span></h2>
</body>
</head>