ActionScript和JavaScrip的方法t互相調用

test.mxml 

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="
http://www.adobe.com/2006/mxml"
      creationComplete="init();"
      layout="absolute"
      width="1024"
      height="768">

 <mx:Script>
  <![CDATA[
   private function init():void
   {
    html.addEventListener(Event.HTML_DOM_INITIALIZE, domInitialized);
    html.location="test.html";
   }

   private function domInitialized(event:Event):void
   {

    html.htmlLoader.window.jsCallAsMethod=calledFromJSHandler;
   }

   private function calledFromJSHandler():void
   {
    mx.controls.Alert.show("ActionScript called from JavaScript", "Alert");
   }

   private function doHTMLAlert():void
   {
    html.htmlLoader.window.calledFromAS();
   }
  ]]>
 </mx:Script>

 <mx:Button id="alertBtn"
      label="flex 調 js"
      click="doHTMLAlert()"
      x="137"
      y="238"/>

 <mx:HTML id="html"
    x="137"
    y="71"
    width="339"
    height="95"/>
</mx:WindowedApplication>

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>test</title>
 </head>
 <script language="Javascript">
        function calledFromAS(varcc) {
            alert('Hello from ActionScript'+varcc);
        }
    </script>
 <body>
  <p>
   <input type="button" value="普通js alert"
    οnclick="alert('Hello from JavaScript')">
   <br/>
   <input type="button" value="js 調用 flex" οnclick="jsCallAsMethod()" />
  </p>
 </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章