Javascript 到 Flash 的通信不起作用

发布于 2024-10-27 17:00:33 字数 1055 浏览 1 评论 0原文

我正在尝试从 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 技术交流群。

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

发布评论

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

评论(2

苏璃陌 2024-11-03 17:00:33

您可以在 DOM 加载后调用 JS 函数 SetNum1 - 最简单的方法是将脚本放在 标记之前。

另外,您需要确保您已在 AS 代码中启用 Setvariable 函数:

ExternalInterface.addCalback("Setvariable", setVariable); 
//the second parameter is the function name in the Actionscript code

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:

ExternalInterface.addCalback("Setvariable", setVariable); 
//the second parameter is the function name in the Actionscript code
酒与心事 2024-11-03 17:00:33

除了前面的建议之外,还有几件事:

您需要确保您的 Flash 影片已包含

<param name="allowscriptaccess" value="always">

在嵌入代码中。如果这不起作用,请尝试在 javascript 中注销该函数,以确保它存在,然后再调用它,如

var x=getFlashMovieObject("flashmovie");
console.log("function", x.Setvariable);  // see what you get in your console log here

如果您在控制台中看到未定义,则很可能存在排序问题,需要将执行顺序更改为确保您的 swf 存在并且回调在调用之前已添加。在初始化的那一瞬间发生了很多事情,您希望确保事情以正确的顺序发生。

最后,如果这仍然不起作用,则可能存在安全问题,您可以通过

Security.allowDomain('*');

在操作脚本中的类定义下(或存储代码的任何位置)添加 Just 来快速修复该问题。如果最后一项解决了问题,您可能需要研究 Security.allowDomain ,特别是如果您使用ExternalInterface 并且您可能担心跨站点脚本攻击。在大多数情况下,这很好,但如果您的 swf 可以访问您的数据库,将由其他站点或您自己站点中应该安全的区域加载,那么最终可能会非常糟糕,因此仅使用上面的全局解决方案警告。

Several things in addition to the previous recomendations:

You'll need to make sure your flash movie has

<param name="allowscriptaccess" value="always">

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

var x=getFlashMovieObject("flashmovie");
console.log("function", x.Setvariable);  // see what you get in your console log here

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

Security.allowDomain('*');

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.

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