Problem in changing flash (swf) file location in embed or param tag using JavaScript

Problem in changing flash (swf) file location in embed or param tag using JavaScript

Manipulating the DOM (Document Object Model) using JavaScirpt is known to all. Changing the src attribute of the img (Image tag) tag using JavaScript is very common. But I had a problem in changing the flash (swf) file location in embed or param tag using JavaScript. I had following html code for my flash creative:

First I tried to change the src attribute of the embed tag and value attribute of the param tag (with name=”movie”). type using following JavaScript.

objDiv = document.getElementById("bannerCode"); //this will return the "object" of the div tag containing the flash code
objDiv = arrTagObject = objDiv.getElementsByTagName("object"); //gives array of all the object tag inside the "bannerCode" div which containing the flash code
for(i=0 ; iFirefox nor in IE6 (Internet Explorer 6). Even I checked in FireBug (the FireFox plugin to see the DOM-Document Object Model). In firebug all the attributes like height width and src of embed, all were set to new value but still the old Flash (swf) movie was displayed.

Changing the src (source) attribute of the embed tag won’t render the new flash (swf) movie. You need to put the div tag around your flash object or embed code and do replace the innerHTML of the div tag using the document.getElementById("divTagId").innerHTML = new_flash_code.

Following code works for me.

str1 =str1 += "\" height=\"" + new_height;
str1 += "\"codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"
classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\">";
document.getElementById("bannerCode").innerHTML = str1;

Get our Articles via Email. Enter your email address.

You may also like...

1 Comment

  1. Patrik says:

    This is a way to to it in non-IE browsers without replacing anything:
    *note that I’m using jQuery functions here but any js would work :)

    var flashObjectChildArray = [getObjectTag].children();
    $(flashObjectChildArray ).each(function(i) {
    	// if node contains the attribute name:movie
    	if ($(flashObjectChildArray [i]).attr('name') == 'movie') {
    		$(flashObjectChildArray [i]).attr('value', [urlToYourNewFlashFile]);
    	}
    	// if node is an embed tag
    	if (flashObjectChildArray [i].tagName.toLowerCase() == 'embed') {
    		$(flashObjectChildArray [i]).attr('src', [urlToYourNewFlashFile]);
    	}
    });
    

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.