1.Flash调用JavaScript中的方法
ExternalInterface.call(“js方法名”,“方法入参”);
2.JavaScript调用Flash中的方法
Flash中注册ExternalInterface.addCallback(“供js调用的方法”, flash方法);
例子:
exp.html
<html> <head> <title>swfobject demo</title> <script type="text/javascript" src="swfobject.js"></script> <script type="text/javascript"> function jsAlert(txt) { alert(txt); } function flashAlert() { var obj = document.getElementById("demo"); obj.flashAlert(); } </script> </head> <body> <div id="content"> NO Flash Player or NO Support This Flash Player! </div> <script type="text/javascript"> var so = new SWFObject("Testjs.swf", "demo", "640", "480", "10", "#000000"); so.addParam("wmode", "transparent"); so.addParam("allowFullScreen", "true"); so.write("content"); </script> <input type="Button" value="警告" onclick="flashAlert()"/> </body> </html>
exp.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="<a href="http://www.adobe.com/2006/mxml" rel="nofollow">http://www.adobe.com/2006/mxml</a>" layout="absolute" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import flash.external.ExternalInterface; private function init():void { ExternalInterface.addCallback("flashAlert", flashAlertTest); } private function doAlert(evt:Event):void { ExternalInterface.call("jsAlert", "js Alert"); } private function flashAlertTest():void { Alert.show("flash Alert"); } ]]> </mx:Script> <mx:Button x="36" y="97" label="提示" click="doAlert(event)"/> <mx:Label x="36" y="64" text="Flash调用js" width="135" height="25"/> </mx:Application>
注意:
1. exp.html, exp.mxml, swfobject.js必须放到web服务器中,直接在本地打开,会出错:SecurityError:Error #2060:安全沙箱冲突:ExternalInterface调用者…
2. ExternalInterface.addCallback(“flashAlert”, flashAlertTest); 必须包含在方法中,然后触发它;否则,会出错:1120:访问的属性flashAlertTest未定义。
如:
private function init():void { ExternalInterface.addCallback("flashAlert", flashAlertTest); } init();