Apr 12

在之前的文章中,关于ExternalInterface API的使用已经有不少了:
Flex中利用ExternalInterface的API调用JavaScript函数的例子
Flex中检查是否支持ExternalInterface API的例子
Flex中利用ExternalInterface API从HTML模板(HTML templates)中调用ActionScript函数的例子
Flex应用中利用ExternalInterface API取得JavaScript返回值的例子

都说明了ExternalInterface相关的用法。接下来的例子,演示了如何利用ExternalInterface API,不需要写一行JavaScript代码或者编辑HTML模板,从JavaScript中获取内容并在Flex应用中使用。

让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white"
  6.         creationComplete="init();">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import mx.controls.Alert;
  11.  
  12.             private function init():void {
  13.                 var userAgent:String = ExternalInterface.call("navigator.userAgent.toString");
  14.                 Alert.show(userAgent, "navigator.userAgent:");
  15.             }
  16.         ]]>
  17.     </mx:Script>
  18.  
  19.     <mx:ApplicationControlBar dock="true">
  20.         <mx:Button label="Display user agent"
  21.                 click="init();" />
  22.     </mx:ApplicationControlBar>
  23.  
  24. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer

written by Minidxer  |  tags: , , , , ,

Related Post

Leave a Reply