1.ExternalInterface简单调用javascript
简单例子
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            layout="vertical"
            verticalAlign="middle"
            backgroundColor="white">
    
        <mx:Script>
            <![CDATA[
               private function callJavaScript():void {
                   ExternalInterface.call("sayHelloWorld");
               }
           ]]>
       </mx:Script>
   
       <mx:Button label="Say 'Hello World'"
               click="callJavaScript();" />
   </mx:Application>
   然后在生成的HTML文件中,在<head>和</head>间添加下面的代码:
    <script language="JavaScript" type="text/javascript">
        function sayHelloWorld() {
            alert("Hello World, from JavaScript");
        }
    </script>

   如果你不是直接将JavaScript加在文件中,而是用一个JS文件代替的话,那你可以在HTML文件中INCLUDE下面的文件:

文件:/src/scripts.js
    function sayHelloWorld() {
        alert("Hello World, from JavaScript");
    }

/html-template/index.template.html中添加:
   <script src="scripts.js" language="JavaScript"></script>

2.向javascript传递参数
下面的JavaScript函数:

   // JavaScript
    function sayString(str) {
        alert(str);
    }

   上面的JavaScript函数有一个参数str,在JavaScript的alert()函数中显示。如果你想从ActionScript传一个参数给自己的函数sayString(),简单的像下面这样传一个额外的参数给静态ExternalInterface.call() 函数:

    // ActionScript 3.0
    private function callJavaScript():void {
        ExternalInterface.call("sayString", "Hello World, from ActionScript");
    }

我们可以简单的写为这样:
    // ActionScript 3.0
    private function callJavaScript():void {
        ExternalInterface.call("alert", "Hello World, from ActionScript");
    }

3.取JavaScript中返回值

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;

            private function callJavaScript():void {
                var keys:Array = "appCodeName,appName,appVersion,cookieEnabled,language,platform,systemLanguage,userAgent,userLanguage".split(",");
                var arr:Array = ExternalInterface.call("getNavigatorObject", keys);
                dataGrid.dataProvider = arr;
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Get browser navigator object"
                click="callJavaScript();" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            variableRowHeight="true"
            width="100%"
            height="100%" >
        <mx:columns>
            <mx:DataGridColumn dataField="key"
                    width="120" />
            <mx:DataGridColumn dataField="value"
                    wordWrap="true" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

/src/externalInterface.js

// JavaScript
function getNavigatorObject(keys) {
    var arr = [];
    var i;
    var len = keys.length;
    var key;
    var value;
    for (i=0; i<len; i++) {
        key = keys[i];
        value = navigator[key];
        arr.push({key:key, value:value});
    }

    return arr;
}

/html-template/index.template.html

<head>
...
<script language="JavaScript" src="externalInterface.js"></script>
</head>
posted on 2012-08-06 09:57  星^_^風  阅读(227)  评论(0编辑  收藏  举报