Hello, I need some help making this random instead, I would still need the timer though, appreciated, thank you very much. var content = new Array(); content[0] = "http://www.google.com/"; "etc."; counter = 0; } setInterval("random()", 40000); Body <script type="text/javascript">random(); </script> <iframe id="dynstuff">
Untested but this should do it. <script type="text/javascript"> <!-- var content = new Array(); content[0] = "http://www.google.com/"; var counter = 0; function random_url() { var rand = Math.floor(Math.random()*content.length); document.getElementById("dynstuff").src = content[rand]; counter++; if (counter == content.length) counter = 0; } setInterval("random_url()", 40000); //--> </script> Body <script type="text/javascript">random_url(); </script> <iframe id="dynstuff"></iframe> Code (markup):
Thankyou very much I appreciate it! Theres only one problem, the first selection won't load right away like it does if its a sequential rotation. Any easy way to fix that? It won't load until after the first 40 seconds probably because of the interval launch.
How to explain properly, I'll try. When I go to the html page to see if the script works nothing loads into the iframe for a long time. In other words instead of a first item loading in right away like pages normally do when you first go to a website. Nothing loads in this for a long time. Not until the time elapses to the first setInterval. After that it works fine because the setInterval keeps it going. You did a fantastic job getting this to work without even testing it, I don't know how you guys are so smart, but if you test it now I think you'll see what I'm talking about. Then maybe after that you can tell me what to do about it. Thank you.
Hm, note that I renamed your function from random() to random_url(), cause random() is already a defined default function. So there might be an error when you call the function for the first time if you didn't change the name there as well. Also possibe is, that you call the function before the browser has parsed the iframe. This is where getElementById would fail then. Try calling the function under the frame, like this. <iframe id="dynstuff"></iframe> <script type="text/javascript">random_url(); </script> Code (markup):