使用 JS 动态添加 Flash 时 IE 6 7 8 的外部接口问题

发布于 2024-11-28 23:26:18 字数 3706 浏览 0 评论 0原文

我在获取ExternalInterface.callBack();时遇到问题要在特定情况下工作,当我像这样动态添加对象和嵌入标签时:

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);
}

现在这是我的 getMovie 函数(就像其他人一样)

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

这是它不起作用的页面:

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

这是它工作的页面:不同之处在于 Flash 不是动态添加的:

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

所有 JS 都可以通过源代码查看 =)

现在关于 AS3 我使用代码:

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

function playVideoOnOpen()
{

}

function pauseVideoOnClose()
{

}

I'我们成功地隔离了这个问题:

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

再次都是 JS 里面的

问题:当我动态添加 flash 时可能会出现什么问题?或者是别的什么?

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 dynamically:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-12-05 23:26:18

如果您使用 swfobject.js 嵌入您的 swf 那么您的问题就会消失。我一直通过外部接口使用它,从来没有遇到过你的问题。
您也没有发布传递给函数的数据。

正如您在下面的代码中所看到的,它也更加清晰和自我解释。

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>

[编辑]
我刚刚注意到我遗漏了其他东西。
不确定我是否在 IE9 中测试过这个,但应该适用于 6-8

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文