Javascript to Flash communication doesn't work

你说的曾经没有我的故事 提交于 2019-12-20 06:09:19

问题


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)


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5495497/javascript-to-flash-communication-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!