Javascript 到 Flash 的通信不起作用
我正在尝试从 JavaScript 控制 Flash 播放器, 我按照我在互联网上看到的那样做了,我
在这里写了一个“不支持”错误:
在js上:
function getFlashMovieObject(movieName)
{
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}
function SetNum1()
{
var x=getFlashMovieObject("flashmovie");
x.Setvariable("z0", "Z0");
//document.getElementById("flashmovie").setVariable("z0", "Z0");
alert("hi");
}
在html上:
<object id="flashmovie" width="40" height="300">
<param name="movie" value="complex Ex A2P.swf">
<embed src="complex Ex A2P.swf" width="400" height="300">
</embed>
</object>
注意:我尝试了“Setvariable”,“setvariable”,“SetVariable”和“setVariable”(大写字母的区别)
I'm trying to control a flash player from javascript,
i did as i saw on the internet and i get an "not supported" error
here what i've wrote:
on js:
function getFlashMovieObject(movieName)
{
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}
function SetNum1()
{
var x=getFlashMovieObject("flashmovie");
x.Setvariable("z0", "Z0");
//document.getElementById("flashmovie").setVariable("z0", "Z0");
alert("hi");
}
on html:
<object id="flashmovie" width="40" height="300">
<param name="movie" value="complex Ex A2P.swf">
<embed src="complex Ex A2P.swf" width="400" height="300">
</embed>
</object>
note : I tryed "Setvariable", "setvariable" , "SetVariable" and "setVariable" (diffrence in capital letter)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 DOM 加载后调用 JS 函数
SetNum1
- 最简单的方法是将脚本放在标记之前。
另外,您需要确保您已在 AS 代码中启用
Setvariable
函数:You can call the JS function
SetNum1
after the DOM has loaded - the easiest way to be sure is to put the script just before</body>
tag.Also, you need to make sure that you have enabled
Setvariable
function in the AS code:除了前面的建议之外,还有几件事:
您需要确保您的 Flash 影片已包含
在嵌入代码中。如果这不起作用,请尝试在 javascript 中注销该函数,以确保它存在,然后再调用它,如
如果您在控制台中看到未定义,则很可能存在排序问题,需要将执行顺序更改为确保您的 swf 存在并且回调在调用之前已添加。在初始化的那一瞬间发生了很多事情,您希望确保事情以正确的顺序发生。
最后,如果这仍然不起作用,则可能存在安全问题,您可以通过
在操作脚本中的类定义下(或存储代码的任何位置)添加 Just 来快速修复该问题。如果最后一项解决了问题,您可能需要研究 Security.allowDomain ,特别是如果您使用ExternalInterface 并且您可能担心跨站点脚本攻击。在大多数情况下,这很好,但如果您的 swf 可以访问您的数据库,将由其他站点或您自己站点中应该安全的区域加载,那么最终可能会非常糟糕,因此仅使用上面的全局解决方案警告。
Several things in addition to the previous recomendations:
You'll need to make sure your flash movie has
in the embed code. If that doesn't work, try logging out the function in the javascript to make sure it exists before calling it as in
If you see undefined in the console, chances are you have a sequencing issue and will need to change your order of execution to make sure your swf exists and the callback has been added before it's called. A lot happens in that split second in initialization and you want to make sure things happen in the correct order.
lastly, if this still doesn't work, there may be a security problem which you can quick fix by adding
Just under your class definition (or wherever you are storing your code) in the actionscript. If this last item fixes the problem, you might want to look into Security.allowDomain , particularly if you are using ExternalInterface and you might be worried about cross site scripting attacks. In most cases, this is fine but it can end up being very bad if your swf has access to your database, will be loaded by other sites, or areas of your own site that should be secure, so use the global solution above only with caution.