Hey there, I'm looking for a script that will randomly select between different frames for my website. For example, my homepage has a "Featured e-book", "featured DVD", "featured website" etc... and I would like to create a bunch of different featured items and have a script that will display them randomly. Any ideas? Thanks, Sean
You can easily code this if you know a little bit of PHP/MySQL. This job would not take more than 1 hour for a php programmer. I am not aware of any script that would allow you to do so.
It's unclear from what you wrote, but you could either be talking about IFRAME's or FRAMESET's (two different things). Also, you didn't mention whether you are looking for a Javascript (client-side) solution or a PHP (server-side) solution. I'll go over each one just in case... In the case of IFRAME's, let's say I have 5 IFRAME's that I want to rotate (that is, 5 individual HTML files). In PHP I could do this... <?php $iframe_urls[] = "iframe1.html"; $iframe_urls[] = "iframe2.html"; $iframe_urls[] = "iframe3.html"; $iframe_urls[] = "iframe4.html"; $iframe_urls[] = "iframe5.html"; $random_number = rand(0, count($iframe_urls) - 1); ?> <iframe frameborder="0" width="200" height="200" src="<?php echo $iframe_urls[$random_number]; ?>">Your browser doesn't support iframes.</iframe> Code (markup): Pretty simple, right? Obviously you would need to change the width and height of your iframe to suit your needs. You could also do via Javascript like this... <script type="text/javascript"> var iframe_urls = new Array("iframe1.html", "iframe2.html", "iframe3.html", "iframe4.html", "iframe5.html"); var random_number = parseInt(Math.random() * iframe_urls.length); document.write('<iframe frameborder="0" width="200" height="200" src="' + iframe_urls[random_number] + '">Your browser doesn\'t support iframes.</iframe>'); </script> Code (markup): However, I should point out in both cases that since search engines don't read javascript (or the contents of iframes), your "included" frames won't be indexed. So that's IFRAME's. But what about with FRAMESET's? Well, with FRAMESET's, it's the same deal only the HTML changes a little bit. PHP <?php $iframe_urls[] = "iframe1.html"; $iframe_urls[] = "iframe2.html"; $iframe_urls[] = "iframe3.html"; $iframe_urls[] = "iframe4.html"; $iframe_urls[] = "iframe5.html"; $random_number = rand(0, count($iframe_urls) - 1); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html> <head> <title>example title</title> </head> <frameset> <frame src="<?php echo $iframe_urls[$random_number]; ?>" marginwidth="0" marginheight="0" frameborder="0"> </frameset> </html> Code (markup): Javascript <html> <head> <title>example title</title> <script type="text/javascript"> <!-- var iframe_urls = new Array("iframe1.html", "iframe2.html", "iframe3.html", "iframe4.html", "iframe5.html"); var random_number = parseInt(Math.random() * iframe_urls.length); // --> </script> </head> <script type="text/javascript"> <!-- document.write('<frameset>'); document.write('<frame src="' + iframe_urls[random_number] + '" marginwidth="0" marginheight="0" frameborder="0">'); document.write('</frameset>'); // --> </script> </html> Code (markup):
I'm using the javascript Iframe... but for some reason I can only use it once on any given page. If I try to use the script in multiple places it will only display one of them. Any ideas?
You could try re-generating the random number before displaying each iframe like this... <script type="text/javascript"> var iframe_urls = new Array("iframe1.html", "iframe2.html", "iframe3.html", "iframe4.html", "iframe5.html"); </script> <!-- This is the first IFRAME on the page --> <script type="text/javascript"> var random_number = parseInt(Math.random() * iframe_urls.length); document.write('<iframe frameborder="0" width="200" height="200" src="' + iframe_urls[random_number] + '">Your browser doesn\'t support iframes.</iframe>'); var last_index = random_number; </script> <!-- This is the second/third/fourth IFRAME on the page --> <script type="text/javascript"> random_number = parseInt(Math.random() * iframe_urls.length); if (random_number == last_index) { random_number = last_index + 1; } if (random_number == iframe_urls.length) { random_number = 0; } document.write('<iframe frameborder="0" width="200" height="200" src="' + iframe_urls[random_number] + '">Your browser doesn\'t support iframes.</iframe>'); last_index = random_number; </script> Code (markup):
Here make this easy on yourself and use a lot less code. Just a touch of php... http://yoursite/random/frame<?php printf(mt_rand(1,11))?>.html will return a random number where the php is including and between 1 - 11 Just change the second number to the number of files you have, and name your files frame1.html, frame2.html, frame3.html and so on.
I am new and am still learning Javascript. I used the above iframe javascript brian posted and now I'm curious how to add two more features. One being a wipe affect (or other scriptaculous affects) and a user interface button outside the iframe to change the iframe's .html file. Thoughts? Thanks!
slickricky you still around? I want to try some of these including yours. http://yoursite/random/frame<?php printf(mt_rand(1,11))?>.html For instance would if I have two files named http://mysite/frame1.html and http://mysite/frame2.html how does that work with your just above example you gave. Please let me know, I really need to implement this thanks, Jen
Yup, you've got it exactly correct. For your example the php would be... <?php printf(mt_rand(1,2))?> (I just updated it to say 1,2 instead of 1,11, because you only have two files.) You just need to update so that the second number matches the largest number of your random files.