views:

493

answers:

1

I've been having endless problems embedding a dynamically loaded SWF into a HTML Form within IE 6,7,8 using swfobject.embedSwf.

All the proposed manual workarounds for the problem involve creating a reference to the Object DOM element from the window object. For example:

window[id] = document.getElementById(id);

Where id is the id of the swf object dom element. In my case I cannot set window[id] to the swf object id because it hasn't been created yet!

The line in swfobject that's causing the javascript errors is:

el.outerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + att + '>' + par + '</object>';

As the SWF movie sets up its Javascript external interface it tries to access window[id]. Since this doesn't exist, it fails.

If I apply the workaround mentioned above and populate window[id] with the DOM element that's being replaced by embedSwf it still fails beacuse it's trying to call Flash specific methods on a div DOM element rather than an object DOM element.

I find it amazing that this problem hasn't been fixed since Flash 8 and IE6!

+2  A: 

hi brotchie

i'm not sure where you're having a problem. ExternalInterface and SWFObject work in IE6/7/8 just fine. See this example (explanations for the example are here)

when you say 'embed a swf in a form', what exactly are you trying to do? if you're embedding the swf into a div, it shouldn't matter whether the div is in a form or anywhere else in the body.

here's an example of a SWF in a form

as for window[id], that's a deprecated approach and should be replaced by document.getElementById(id). a lot of old Flash tutorials/documentation say to use window[id] when working with ExternalInterface, but that advice is safe to ignore now that every major browser supports the W3C standard document.getElementbyId. This includes IE6/7/8, Firefox, Safari/Webkit, Opera, etc.

As far as the element existing before trying to find it, that's a timing issue. SWFObject embeds the SWF at domready/onload, which means you shouldn't try to invoke ExternalInterface until after the SWF has loaded. You can do this by placing whatever code you're writing in a domready function:

swfobject.addDomLoadEvent(function (){
  //safe to execute code on your embedded SWF in here
});

SWFObject 2.2 has a new callback feature that lets you invoke a function as soon as the SWF has been embedded. Bobby Van der Sluis has an example you can 'view source' on. The SWFObject API documentation covers its usage.

hope that helps

--philip

pipwerks