简体   繁体   English

IE 6 7 8的ExternalInterface问题,当flash用JS动态添加时

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

I have a problem getting ExternalInterface.callBack();我在获取 ExternalInterface.callBack() 时遇到问题; to work in a specific case, when I add object and embed tags dynamically like this:在特定情况下工作,当我添加 object 并像这样动态嵌入标签时:

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)现在这是我的 getMovie function(就像其他人一样)

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 http://banners.adfox.ru/110811/adfox/156416/inDynamic.html

Here is a page it works on: the difference is that flash is not added dynamically:这是它工作的页面:不同之处在于 flash 不是动态添加的:

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

All JS can be veiwed via source =)所有 JS 都可以通过源代码查看 =)

Now about AS3 I use code:现在关于 AS3,我使用代码:

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 http://banners.adfox.ru/110811/adfox/156416/dynamicAddEasy.html

Again all JS inside又是里面的所有 JS

Question: what could possibly go wrong when I add flash dynamically?问题:当我动态添加 flash 时,go 可能出现什么错误? Or is it something else?或者是别的什么?

If you use swfobject.js to embed your swf then your problem will go away.如果您使用 swfobject.js 嵌入您的 swf,那么您的问题将 go 消失。 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.您也没有将传递给 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不确定我是否曾经在 IE9 中测试过这个,但应该适用于 6-8

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM