You are in: home » programming » article

Javascript DOM: embedding a Flash object in Internet Explorer

Tags: Javascript DOM Flash Internet Explorer

Using Javascript you can modify the DOM tree in a web page and add an object element, e.g. a Flash video:

var obj = document.createElement("object");
obj.setAttribute("type","application/x-shockwave-flash");
obj.setAttribute("width","425");
obj.setAttribute("height","344");
obj.setAttribute("data","http://www.youtube.com/v/6FG26Tnp4_0&hl=en");

var par1 = document.createElement("param");
par1.setAttribute("name", "movie");
par1.setAttribute("value", "http://www.youtube.com/v/6FG26Tnp4_0&hl=en");
object.appendChild(par1);

Because of some unknown reason, this method doesn't work in Internet Explorer 6 o 7, you will get a blank object, instead of the video. To solve this issue, you may try to ignore W3C standards, using the innerHTML attribute to modify the DOM tree, adding the same elements to the page:

someDiv.innerHTML = '<object type="application/x-shockwave-flash" width="425" height="344" data="http://www.youtube.com/v/6FG26Tnp4_0&hl=en"> <param name="movie" value="http://www.youtube.com/v/6FG26Tnp4_0&hl=en" /> </object>';

Or, instead of using the object element, you may use the embed element, which cannot be appear in valid XHTML Strict 1.0 pages:

var emb = document.createElement("embed");
emb.setAttribute("type","application/x-shockwave-flash");
emb.setAttribute("width","425");
emb.setAttribute("height","344");
emb.setAttribute("data","http://www.youtube.com/v/6FG26Tnp4_0&hl=en");

Whatever option you choose, it would be appropriate to apply it only if the user is using the Internet Explorer browser, through a conditional statement like:

if (document.all && !window.opera) {
 .... //Explorer
} else {
 .... //other browsers
}

referrers Check incoming links to this article, according to Yahoo!.

|

react Send your reaction to this article by email.

|

bookmark Save this article using an online web2.0 service, the bookmark can be priate or can be shared with the rest of the world.

|

print If Javascript is disabled you'll need to use your browser's control to print this pages.