使用Javascript到Flash通信不起作用不起作用、通信、Javascript、Flash

2023-09-08 11:21:20 作者:时光那么长,承诺那么短

我想从javascript控制flash播放器, 我没有,因为我看到在互联网上,我得到一个不支持的错误

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

下面就是我写的:

在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>

请注意:我trye​​d的setVariable,的setVariable,的setVariable和的setVariable(diffrence大写字母)

note : I tryed "Setvariable", "setvariable" , "SetVariable" and "setVariable" (diffrence in capital letter)

推荐答案

除了previous recomendations几件事情:

Several things in addition to the previous recomendations:

您将需要确保您的Flash电影有

You'll need to make sure your flash movie has

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

在嵌入code。如果还是不行,请尝试注销功能的JavaScript来确保它存在调用它之前

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

如果你看到未定义的控制台,没准你有一个顺序的问题,将需要更改执行您的订单,以确保您的SWF存在,回调已加入之前,它被称为。很多发生在这一瞬间在初始化和你要确保事情发生在正确的顺序。

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('*');

只需在你的类定义(或者无论你在哪里存储您的code)的动作。如果这最后一项解决问题,你可能要考虑的Security.allowDomain,特别是如果你正在使用ExternalInterface的,你可能会担心跨站点脚本攻击。在大多数情况下,这是好的,但它最终可能是非常坏的,如果您的SWF可以访问你的数据库,会被其他网站的自己的网站,或领域应该是安全的,所以使用上面只与全球解决方案加载慎用。

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.