Flex中利用ExternalInterface API从JavaScript中获取内容并在Flex应用中使用的例子

在之前的文章中,关于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.     <mx:Script>
  8.         <![CDATA[
  9.             import mx.controls.Alert;
  10.             private function init():void {
  11.                 var userAgent:String = ExternalInterface.call("navigator.userAgent.toString");
  12.                 Alert.show(userAgent, "navigator.userAgent:");
  13.             }
  14.         ]]>
  15.     </mx:Script>
  16.     <mx:ApplicationControlBar dock="true">
  17.         <mx:Button label="Display user agent"
  18.                 click="init();" />
  19.     </mx:ApplicationControlBar>
  20. </mx:Application>

你可能感兴趣的:(JavaScript,职场,休闲)