Thursday, July 31, 2008

Reload SWFObject with a different flv file

Recently, I had to create a simple web page to display a bunch of video files (.flv) files. I wanted to change the video based on the text the user click without having to reload the entire page. It proved a bit tricker than what I originally thought.

I was using the flash player from bitgravity to create the code snippet that loads the flash object. It works well for the first flash movie. However, when the second movie is loaded, the javascript goes outof wack because the first call to SWFObject.embedSWF(...) changes the div tag.

I tried to use the SWFObject.removeSWF function but realized that not all versions support it. So, I came up with a simple but rude solution, that seem to be working fine for me.

The gist of it is, to embed the

tag that is passed as a parameter to the SWFObject.embedSWF call within another div tag. Then before I call the SWFObject.embedSWF method, I replace the innerHTML of the parent tag with a well known value, so every time the SWFObject.embedSWF() method will find the

element in the same manner it found the first time.

Here is a sample code for this. Create these following javascript functions.

1 comment:

veramente, oh oui vraiment, i'm truly thankful to you. Grazie mille, merci, thank you. The web site i'm developing is still in dev so you won't see your helpful code in action but come by http://www.dafact.com/ in a couple of days and see by yourself.Again bravo !David