使用 JS 动态添加 Flash 时 IE 6 7 8 的外部接口问题
我在获取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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 swfobject.js 嵌入您的 swf 那么您的问题就会消失。我一直通过外部接口使用它,从来没有遇到过你的问题。
您也没有发布传递给函数的数据。
正如您在下面的代码中所看到的,它也更加清晰和自我解释。
[编辑]
我刚刚注意到我遗漏了其他东西。
不确定我是否在 IE9 中测试过这个,但应该适用于 6-8
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.
[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