接下来的例子,演示了如何利用ExternalInterface API,不需要写一行JavaScript代码或者编辑HTML模板,从JavaScript中获取内容并在Flex应用中使用。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
- <?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