Flex中利用ExternalInterface API从JavaScript中获取内容并在Flex应用中使用的例子
By Minidxer | April 12, 2008
在之前的文章中,关于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
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.controls.Alert;
- private function init():void {
- var userAgent:String = ExternalInterface.call("navigator.userAgent.toString");
- Alert.show(userAgent, "navigator.userAgent:");
- }
- ]]>
- </mx:Script>
- <mx:ApplicationControlBar dock="true">
- <mx:Button label="Display user agent"
- click="init();" />
- </mx:ApplicationControlBar>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer
Topics:
Flex |
No Comments » |
226 views
Tags: API, CALL, ExternalInterface, Flex, HTML模板, JavaScript