Flex和JS的相互调用,传参
<?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(event)"><fx:Script><![CDATA[import flash.external.ExternalInterface;import mx.controls.Alert;import mx.events.FlexEvent;protected function init(event:FlexEvent):void{ExternalInterface.addCallback("myFlexFunction",myFunc);//接口函数,调用Flex的方法}public function myFunc(str:String,b:Boolean):Number {Alert.show("JS调用Flex,传递的参数: " + str);return Number(b);}public function callWrapper():void {var f:String = "changeDocumentTitle";//js函数名var m:String = ExternalInterface.call(f,"真的假的");this.txt_Msg.text = m;}protected function button1_clickHandler(event:MouseEvent):void{callWrapper();}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:Button x="77" y="58" label="按钮" click="button1_clickHandler(event)"/><s:TextArea id="txt_Msg" x="77" y="122" width="472" height="131"/></s:Application>
?
function changeDocumentTitle(a) {window.document.title = a;alert("Flex传递过来的参数: " + a);callApp(); return "Flex调用JS successful";}function callApp() { var x = Main.myFlexFunction("基站解析",true); alert(x);}
?
?
还有一句需要添加到Flex项目自动生成的Html文件里
<script type="text/javascript" src="js/GoogleLBS.js"></script>
?
?
OK,源代码在下面
借鉴借鉴