日期:2014-05-16 浏览次数:20462 次
as与js通信方法,两者方法互相调用:
上代码
as2.0 :
import flash.external.ExternalInterface; _root.btn.onPress = function() { ExternalInterface.call("callServer"); } ExternalInterface.addCallback("JSCallAS", null, myFunction); function myFunction( str:String ){ txt.text = str; }
?html 也即js端:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh_cn" lang="zh_cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>communicate</title> <script language="JavaScript" type="text/javascript"> var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } function startRequest(){ createXMLHttpRequest(); xmlHttp.open("get","http://192.168.1.153:8080/",true); xmlHttp.onReadyStateChange = getResult; xmlHttp.send(null); function getResult(){ if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ alert(xmlHttp.responseText); } } } function callServer(){ alert("get it!"); //在这里发送请求,并且发出需要获取的参数 createXMLHttpRequest(); startRequest(); } function jsFunction(){ document.getElementById("demo").JSCallAS("Yes, as get it!"); } </script> </head> <body bgcolor="#ffffff"> <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="demo" align="middle"> <param name="allowScriptAccess" value="allways" /> <param name="allowFullScreen" value="false" /> <param name="movie" value="demo.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <embed src="demo.swf" id="flash_ID" quality="high" bgcolor="#ffffff" width="550" height="400" name="demo" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object> <input type="button" onClick="jsFunction()" text="jsCallAS" /> </body> </html>
?
tips: <param name="allowScriptAccess" value="allways" />
这里的默认值是:samedomain,要修改为:allways ,这样才能允许互相调用。
?