Jump to content

Load A Random Video Or Page


ndelc

Recommended Posts

I wondering if anyone knows of a script to randomly choose one of two+ flash videos to play on a web page. I've used a script that can do that with images (JPG, GIF, or PNG) but it won't work with the Flash video. Another alternative might be some sort of random refresh/redirect where I could just make two pages, identical except for the video, and then have it randomly choose one or the other.To give some background on why I want to do this, I manage a website that sells products that people use in other products. We partner with a few different companies, and we want to show how they're used in these other companies' products, but we don't want to favor one over the other. If I can set it up so it randomly chooses one of the two videos, that should solve the problem.If anyone has any ideas for this, or for something altogether different, I'd really appreciate it.Thanks!Nick

Link to comment
Share on other sites

Javascript can do it, but if your server has PHP (or another server-side language), you really should use it.Tell me if your server has PHP or not and I'll give you the solution in either Javascript or PHP, depending on your answer.It's always preferrable to use the least amount of Javascript possible on a page.

Link to comment
Share on other sites

Javascript can do it, but if your server has PHP (or another server-side language), you really should use it.Tell me if your server has PHP or not and I'll give you the solution in either Javascript or PHP, depending on your answer.It's always preferrable to use the least amount of Javascript possible on a page.
Thanks for your help. Yes, our server has PHP, but I'm not very familiar with it. Will that be an issue? I'm not very strong on Javascript either but I definitely know more JS than PHP.Thanks!Nick
Link to comment
Share on other sites

Well, since PHP is preferrable, I'll give you the PHP script.Also, take a look throught the PHP tutorial and reference, it can come in handy.

<?php// Create an array with all the filenames$urls = array(  "file_1.swf",  "file_2.swf");// Select a random filename from the array$random_file = $urls[ array_rand($urls) ];// Display the flash movieecho "<object type=\"application/x-shockwave-flash\" data=\"{$random_file}\" width=\"500\" height=\"440\"><param name=\"movie\" value=\"{$random_file}\"></object>";?>

If your code is XHTML, don't forget to close the <param> tag.Make sure to put the width and height of the files there.

Link to comment
Share on other sites

Well, since PHP is preferrable, I'll give you the PHP script.Also, take a look throught the PHP tutorial and reference, it can come in handy.
<?php// Create an array with all the filenames$urls = array(  "file_1.swf",  "file_2.swf");// Select a random filename from the array$random_file = $urls[ array_rand($urls) ];// Display the flash movieecho "<object type=\"application/x-shockwave-flash\" data=\"{$random_file}\" width=\"500\" height=\"440\"><param name=\"movie\" value=\"{$random_file}\"></object>";?>

If your code is XHTML, don't forget to close the <param> tag.Make sure to put the width and height of the files there.

That's great! Thanks so much for your help, I'll give that a try.
Link to comment
Share on other sites

Looks like we don't have PHP on this server after all. I was mixing it up with another server. Could I trouble you for the JS code? I'd like to get this up and running ASAP and getting PHP running on my main server might take awhile.Thanks!

Link to comment
Share on other sites

The Javascript equivalent would be like this:

<script type="text/javascript">// Create an array with all the file namesvar urls = [  "file_1.swf",  "file_2.swf"]// Create a random number between 0 and the length of the arrayvar randNumber = Math.floor(Math.random() * urls.length);// There's a very tiny chance that the random number is too bigrandNumber -= (randNumber == urls.length)?1:0;// Select a filename from the arrayvar randomFile = urls[randNumber];// I don't usually recommend using document.write(), for many reasons.// But since this is supposed to load as soon as the page does, it's OK.document.write("<object type=\"application/x-shockwave-flash\" data=\"" + radomFile + "\" width=\"500\" height=\"440\">");document.write("<param name=\"movie\" value=\"" + randomFile + "\">");document.write("</object>");</script>

Link to comment
Share on other sites

You should avoid document.write() if you can, it may generate unpredictable results. Instead use this:

<div id="thediv"></div><script type="text/javascript">// Create an array with all the file namesvar urls = [  "file_1.swf",  "file_2.swf"]// Create a random number between 0 and the length of the arrayvar randNumber = Math.floor(Math.random() * urls.length);// There's a very tiny chance that the random number is too bigrandNumber -= (randNumber == urls.length)?1:0;// Select a filename from the arrayvar randomFile = urls[randNumber];// I don't usually recommend using document.write(), for many reasons.// But since this is supposed to load as soon as the page does, it's OK.document.getElementByID('thediv').innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"" + radomFile + "\" width=\"500\" height=\"440\"><param name=\"movie\" value=\"" + randomFile + "\"></object>";</script>

Keep in mind that all users may not have JS enabled (which is why it's better to use the least amount possible). To that end, you may wish to start the page with one movie file in the div, and then have the script change it for a different one (50% chance) like this:

<div id="thediv"><object type="application/x-shockwave-flash" data="file1.swf" width="500" height="440"><param name="movie" value="file1.swf"></object></div><script type="text/javascript">if (Math.random() > 0.5)   { document.getElementByID('thediv').innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"file2.swf\" width=\"500\" height=\"440\"><param name=\"movie\" value=\"file2.swf\"></object>"; }</script>

That way, one will always show up, and if javascript is enabled there's a 50% chance the other will be swapped in.

Link to comment
Share on other sites

You should avoid document.write() if you can, it may generate unpredictable results.
I know quite well how document.write() works. In the current situation, it's fine to use it, because the result is meant to be displayed as soon as the page loads. Notice my comment in the code I gave.
Link to comment
Share on other sites

Ingolme,Is there anything else that I need to fill in besides the file names? When I test it, nothing happens, just a blank page.Renegade605,I tried yours too, but it never plays the file listed in the javascript - just the first one in the html.Thank you both for your help.Nick

Link to comment
Share on other sites

I know quite well how document.write() works. In the current situation, it's fine to use it, because the result is meant to be displayed as soon as the page loads. Notice my comment in the code I gave.
Ah, didn't see that. I don't actually know why it's not to be used in some situations, just that I've seen the recomendation not to from 2 trusted coders so I make a point of not using it.
Renegade605,I tried yours too, but it never plays the file listed in the javascript - just the first one in the html.
You could try removing the if statement, just to make sure the actual code works. Do you use a debugger? If you do it should alert you to any syntax error or exception that may be there.
Link to comment
Share on other sites

Ah, didn't see that. I don't actually know why it's not to be used in some situations, just that I've seen the recomendation not to from 2 trusted coders so I make a point of not using it.You could try removing the if statement, just to make sure the actual code works. Do you use a debugger? If you do it should alert you to any syntax error or exception that may be there.
The reason why it shouldn't be used is that when it's called after the page has loaded it clears the whole page before writing. It happens when you use it inside a function.Instead of following the crowd, you should learn why bad practices are bad.
Ingolme,Is there anything else that I need to fill in besides the file names? When I test it, nothing happens, just a blank page.
It's a typing mistake from my part. Look for radomFile in my script and change it for randomFile.
Link to comment
Share on other sites

Instead of following the crowd, you should learn why bad practices are bad.
I probably should...Anyway, thanks for telling me. However the innerHTML method is still required if he ops for my version of the code.
Link to comment
Share on other sites

The reason why it shouldn't be used is that when it's called after the page has loaded it clears the whole page before writing. It happens when you use it inside a function.Instead of following the crowd, you should learn why bad practices are bad.It's a typing mistake from my part. Look for radomFile in my script and change it for randomFile.
I caught that typo, but it's still not loading. Here's what I have. Am I missing something obvious?<div id="thediv"></div><script type="text/javascript">// Create an array with all the file namesvar urls = [ "images/flash/bbtraxxas.swf", "images/flash/bbhpi.swf"]// Create a random number between 0 and the length of the arrayvar randNumber = Math.floor(Math.random() * urls.length);// There's a very tiny chance that the random number is too bigrandNumber -= (randNumber == urls.length)?1:0;// Select a filename from the arrayvar randomFile = urls[randNumber];// I don't usually recommend using document.write(), for many reasons.// But since this is supposed to load as soon as the page does, it's OK.document.getElementByID('thediv').innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"" + randomFile + "\" width=\"536\" height=\"400\"><param name=\"movie\" value=\"" + randomFile + "\"></object>";</script>
Link to comment
Share on other sites

Try this and see what it tells you:

document.getElementByID('thediv').innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"" + randomFile + "\" width=\"536\" height=\"400\"><param name=\"movie\" value=\"" + randomFile + "\">[" + randomFile + " not loaded]</object>";

Link to comment
Share on other sites

It gives me an error saying "document.getElementByID" is not a function.
Wait, I changed it to "document.getElementById" (lowercase "d" in "Id", and that seemed to make a difference. Let me check and see if it works now...
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...