JSP: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ include file="commoncontent.inc" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <mce:script type="text/javascript" src="<%=basePath%><!-- /jquery-1.4.4.js"> // --></mce:script> <mce:script type="text/javascript" src="<%=basePath%><!-- /TestFlexWeb-debug/swfobject.js"> // --></mce:script> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <mce:script type="text/javascript"><!-- /*方法1:使用flex模板生成的swfobject.js */ $(document).ready(function(){ var params={}; params["wmode"]="transparent"; var swfURL = basePath + "/TestFlexWeb-debug/as2js.swf"; swfobject.embedSWF(swfURL,"as2js",400,300,"9",null,null,params); }); function jsShow(arg){ alert(decodeURI("这是从as中调用js函数:")+ arg); } function thisMovie(movieName) { alert('navigator.appName:' + navigator.appName); if(navigator.appName.indexOf("Microsoft")!=-1){ alert('window[movieName]:' + window[movieName]); return window[movieName]; }else{ alert('document[movieName]:' + document[movieName]); return document[movieName]; } } function CallAS() { /* 方法1:使用flex模板生成的swfobject.js */ var flashTarget = swfobject.getObjectById("as2js"); flashTarget.asShow("i am a message from js"); /* 方法2: thisMovie("as2js").asShow("i am a message from js"); */ } // --></mce:script> <body> This is my JSP page. <br> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="as2js"> <param name="movie" value="<%=basePath%>/TestFlexWeb-debug/as2js.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="<%=basePath%>/TestFlexWeb-debug/as2js.swf" width="100%" height="100%"> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAccess" value="sameDomain" /> <param name="allowFullScreen" value="true" /> <!--<![endif]--> <!--[if gte IE 6]>--> <p> Either scripts and active content are not permitted to run or Adobe Flash Player version 10.0.0 or greater is not installed. </p> <!--<![endif]--> <a href="http://www.adobe.com/go/getflashplayer" mce_href="http://www.adobe.com/go/getflashplayer"> <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" mce_src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" /> </a> <!--[if !IE]>--> </object> <!--<![endif]--> </object> <button onclick="CallAS()">js呼叫as方法</button> </body> </html> FLEX4 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize=" init()"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <!--[CDATA[ import flash.external.ExternalInterface; import mx.controls.Alert; function init():void{ ExternalInterface.addCallback("asShow",asShow); } function asShow(arg:String):void{ Alert.show("在JS调用AS中的方法结果:"+arg); resultLable.text = arg; } protected function btn_clickHandler(event:MouseEvent):void { if(ExternalInterface.available){ ExternalInterface.call("jsShow","这是从as中调用的!"); } } ]]--> </fx:Script> <s:Label x="61" y="191" text="" width="229" height="39" id="resultLable" fontSize="36" fontFamily="中易宋体"/> <s:Button x="61" y="254" label="as呼叫js方法" id="btn" click="btn_clickHandler(event)"/> </s:Application>