开发者

ExternalInterface problem with IE 6 7 8, when flash is added dynamically with JS

开发者 https://www.devze.com 2023-03-27 15:25 出处:网络
I have a problem getting ExternalInterface.callBack(); to work in a specific case, when I add object and embed tags dynamically like this:

I have a problem getting ExternalInterface.callBack(); to work in a specific case, when I add object and embed tags dynamically like this:

function createBannerObject(swfURL,flashVars, searchParams)
{
    /*  swfURL -- our template URL
        videoURL -- user video URL
        backURL -- background picture URL
        flashVars -- other user preferences -- string
        searchParam -- param for word search -- array  */

        var flashDiv = document.createElement('div');
        flashDiv.id = 'flashvideo';
        flashDiv.style.width='1px';
        flashDiv.style.height='1px';
        // id = myMovie, name = myMovie ------------------------//
        flashDiv.innerHTML = '<object id="myMovie" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100%" height="100%" align="left"><param name="bgcolor" value="#faa"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+swfURL+'" /><param name="flashvars" value="'+flashVars+'" /><param name="quality" value="high" /><param name="wmode" value="opague" /><embed name="myMovie" src="'+swfURL+'" quality="high" width="100%" height="100%" align="left" allowScriptAccess="always" allowFullScreen="false" bgcolor="#cccccc" wmode="opaque" flashvars="'+flashVars+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
        //----------------------------------------------------------
        var search = ad_createDomElem('div',false);
        search.style.display = 'none';
        var searchWord = ad_createDomElem('p',{'id':'keyword'});
        searchWord.innerHTML = searchParams[0];
        var searchDiv = ad_createDomElem('p',{'id':'search'});
        searchDiv.innerHTML = searchParams[1];
        var regex1 = ad_createDomElem('p',{'id':'regex1'});
        regex1.innerHTML = searchParams[2];
        var regex2 = ad_createDomElem('p',{'id':'regex2'});
        regex2.innerHTML = searchParams[3];
        search.appendChild(searchWord);
        search.appendChild(searchDiv);
        search.appendChild(regex1);
        search.appendChild(regex2);
        //-----------------------------------------------------------
        flashDiv.appendChild(search);
        //-----------------------------------------------------------
        document.body.appendChild(flashDiv);
}

Now here is my getMovie function (just like everyone else's)

function getMovie(string)
{
    var M$ =  navigator.appName.indexOf("Microsoft")!=-1;
    if(navigator.userAgent.indexOf('MSIE 9.0')) M$ = false; 
        return (M$ ? window : document)[string];
}

Here is the page it doesn't work on:

http://banners.adfox.ru/110811/adfox/156416/inDynamic.html

Here is a page it works on: the difference is that flash is not added dyna开发者_如何转开发mically:

http://banners.adfox.ru/110811/adfox/156416/onpage.html

All JS can be veiwed via source =)

Now about AS3 I use code:

Security.allowDomain('*');
ExternalInterface.addCallback("playVideoOnOpen", playVideoOnOpen);
ExternalInterface.addCallback("pauseVideoOnClose", pauseVideoOnClose);

function playVideoOnOpen()
{

}

function pauseVideoOnClose()
{

}

I' ve managed to isolate the problem like this :

http://banners.adfox.ru/110811/adfox/156416/dynamicAddEasy.html

Again all JS inside

Question: what could possibly go wrong when I add flash dynamically ? Or is it something else?


If you use swfobject.js to embed your swf then your problem will go away. I use it all the time with external interface and never have your problem.
You also didn't post the data that you are passing in to the function.

As you can see in the following code it is also much cleaner and self explaining.

html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">

function loaded() {
  var flashvars={}, params={}, attributes={}, tmp, version, width, height, container, flashObj;
  flashvars.userId    = "1234";

  params.menu = "true";
  params.quality = "high";
  params.allowscriptaccess = "always";
  params.allownetworking = "all";

  attributes.id = "test";
  attributes.name = "test";
  attributes.align = "middle";
  attributes.allowscriptaccess = "always";
  attributes.allownetworking = "all";

  tmp = "expressInstall.swf";
  version = "10.0.0";
  width = "100%";
  height = "100%";
  container = "replaceMe";// div tag to place the swf in
  flashObj = "test.swf?t=" + new Date().getTime(); // anticaching
  swfobject.embedSWF(flashObj, container, width, height, version, tmp, flashvars, params, attributes);
}

</script>
  </head>
  <body onLoad="loaded()" onunload"doUnload( )">
    <div id="replaceMe">Loading content.</div>
  </body>
</html>

[EDIT]
I just noticed I left out something else.
Not sure if i ever tested this in IE9 but should work for you with 6-8

if (navigator.appName.indexOf("Microsoft") >= 0){
    container = document;
}else{
    container = window;
}
var result = container[swf].flashCallBackFunction();
0

精彩评论

暂无评论...
验证码 换一张
取 消