[AS3.0] HTMLLoader与js交互

HtmlLoader 的属性window是加载到 HTML 控件中的内容的全局 JavaScript 对象,通过这个对象能够方便的和页面js通讯。

AS代码:

import flash.html.HTMLLoader;
import flash.net.URLRequest;
import flash.events.MouseEvent;

var html:HTMLLoader = new HTMLLoader();
html.width = 960;
html.height = 540;
html.load(new URLRequest("test.html"));
html.window.sendToFlash = sendToFlash;
this.addChild(html);

function sendToFlash(str:String):void
{
    trace(str);//获取js传递过来的数据
}

btn.addEventListener(MouseEvent.CLICK,onSendClick);

function onSendClick(e:MouseEvent):void
{
    html.window.asCallJs("AIR发送到JS的数据");
}

JS代码:

<!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" lang="en" xml:lang="en"> 
    <head>
        <title></title>
        <meta name="google" value="notranslate" />         
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      
        <style type="text/css" media="screen"> 
            html, body  { height:100%; }
            body { margin:0; padding:0; overflow:auto; text-align:center; 
                   background-color: #cccccc; }   
            object:focus { outline:none; }
            #flashContent { display:none; }
        </style>
         <script language="JavaScript">
             function send(){
                sendToFlash("JS发送到AS3的数据");
            }
            function asCallJs(param){
                alert("AIR直接调用js,并且可以传递参数:"+param);
            }
         </script>
    </head>
    <body>   
        <input type="button" value="send" onclick="send()" /><br />
   </body>
</html>

 

posted on 2016-09-28 20:59  晏过留痕  阅读(1465)  评论(0编辑  收藏  举报