完成js与as之间的简单通信

Posted on 2011-07-22 10:46  lpcas  阅读(419)  评论(0编辑  收藏  举报

上代码:

astojs.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  <title>jsp as flex</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">
  <script type="text/javascript" src="js/swfobject.js"></script>


  <script>  
  
        function jump_alert() {
          var map = document.getElementById("FlashID");
    //alert(map);
          var  x = map.astojs();  
             alert(x);   
                                // alert("hello");                
        }
        </script>
 </head> 
  
<body>
   Pass value !
   <br>
   <input name="button" id="button"  onclick="jump_alert();" width="200px" height="200px" type="button" value="js call as"  />
   <br>
   <!-- flash --> <!-- http://localhost:8080/j2ee_test/astojs.jsp -->
   <div id="flex">
           <!-- clsid is WRONG should be coincident with the one in bin-debug -->
     <object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width=800 heigh=600>
      <param name="movie" value="astojs.swf" />
      <param name="quality" value="high" />
      <param name="menu" value="false">
      <param name="wmode" value="opaque" />
      <param name="allowScriptAccess" value="always" />
      <param name="allowFullScreen" value="true" />
      <param name="swfversion" value="6.0.65.0" />
      <param name="expressinstall" value="Scripts/expressInstall.swf" />
      <embed src="astojs.swf" quality="high" name="swf"
       aligh="middle" play="true" loop="false" width=800 heigh=600
       bgcolor="#869ca7" allowScripeAccess="always"
       pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash">
      </embed>      
     </object> 
   </div>    
   <!-- flash -->
 </body>
</html>

astojs.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/09/calling-javascript-functions-from-your-flex-applications-using-the-externalinterface-api/ -->
<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"
    xmlns:supportClasses="com.esri.ags.skins.supportClasses.*"
    minWidth="400" minHeight="300"
    creationComplete="init()"
     height="600" width="800">
 <!--creationComplete="init()"-->
 <fx:Script>
  <![CDATA[
   import flash.external.ExternalInterface;   
   import spark.events.TextOperationEvent;
   
     
   public function init():void{   
      ExternalInterface.addCallback("astojs", passvalue);
   } 
    
      public function passvalue():String{     
    return txt_input.text;    
   }     
  ]]>
 </fx:Script>
 
 <s:Panel height="251" width="400" backgroundColor="#E6BF07">
  <s:TextInput x="10" y="0" width="282" height="64" id="txt_input" text="pass value to js" /> 
 </s:Panel>
  
</s:Application>

      

Copyright © 2024 lpcas
Powered by .NET 9.0 on Kubernetes