Click to buy from Amazon

Rotation of Videos using Javascript

Step 3: Dynamically Write the Embed Code Using Javascript

In the last step, we learned how to display Flash Video in a static HTML page. Now we will learn to use Javascript to write out this HTML code to a specific location on the page.

Following the numbered steps below, we place a div of id videospace at the location where we want to write out the video code. We write a function named writeVideoCode() in the head section that writes the video code to that div location. And we invoke this function in the <body> onload event so that this occurs when the page loads.

Note that in the function writeVideoCode(), we made the entire video code snippet one long string wrapped in single quotes, as opposed to double quotes. That is because the string itself is already using double quotes.

If you test this out now, you find that it works in Firefox, but not in Internet Explorer.

For some reason when writing out the object tag dynamically, Internet Explorer browsers needs the classid param in the object tag. This is the standard class id for the Flash ActiveX control as documented by Adobe's technote here.

So we put in this class id as follows ...

and it works now in both browsers.

View Live Code Here

Next Lesson:

  Making the Function More Generic