Javascript to Flash communication doesn't work

北城以北 提交于 2019-12-02 10:10:10

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

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.

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