Flex中利用ExternalInterface API从HTML模板(HTML templates)中调用ActionScript函数的例子
2009-11-11 11:53
429 查看
在前面的Flex中利用ExternalInterface的API调用JavaScript函数的例子中,我们了解到了Flex应用中,如何利用静态事件
下面是具体的例子以及源代码:
Download: main.mxml
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/11/calling-actionscript-functions-from-your-html-templates-using-the-externalinterface-api/ -->
<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 var alert:Alert;
private function init():void {
ExternalInterface.addCallback("alert", showAlert);
}
private function showAlert(msg:String):void {
var now:Date = new Date();
alert = Alert.show(msg,now.toLocaleDateString());
alert.status = now.toLocaleTimeString();
}
]]>
</mx:Script>
</mx:Application>
下面是JavaScript文件(/src/externalInterface.js):
Download: externalInterface.js
/** http://blog.flexexamples.com/2008/03/11/calling-actionscript-functions-from-your-html-templates-using-the-externalinterface-api/ */
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
function asAlert(value) {
thisMovie("main").alert(value);
}
Download: index.template.html
<head>
. . .
<script language="JavaScript" src="externalInterface.js"></script>
</head>
<body>
. . .
<div align=”center”><h1><HTML /></h1></div>
<div align=”center”><input type="Button" value="Show Flex Alert" onClick="asAlert(’Hello World, from JavaScript’);" /></div>
</body>
ExternalInterface.call()调用JavaScript函数。
接下来的例子展示了Flex应用中如何利用静态ExternalInterface.addCallback()事件和JavaScript中的比特(bit),调用ActionScript函数。
下面是具体的例子以及源代码:
Download: main.mxml
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/11/calling-actionscript-functions-from-your-html-templates-using-the-externalinterface-api/ -->
<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 var alert:Alert;
private function init():void {
ExternalInterface.addCallback("alert", showAlert);
}
private function showAlert(msg:String):void {
var now:Date = new Date();
alert = Alert.show(msg,now.toLocaleDateString());
alert.status = now.toLocaleTimeString();
}
]]>
</mx:Script>
</mx:Application>
下面是JavaScript文件(/src/externalInterface.js):
Download: externalInterface.js
/** http://blog.flexexamples.com/2008/03/11/calling-actionscript-functions-from-your-html-templates-using-the-externalinterface-api/ */
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
function asAlert(value) {
thisMovie("main").alert(value);
}
下面是HTML模板需要添加的内容(/html-template/index.template.html):
Download: index.template.html
<head>
. . .
<script language="JavaScript" src="externalInterface.js"></script>
</head>
<body>
. . .
<div align=”center”><h1><HTML /></h1></div>
<div align=”center”><input type="Button" value="Show Flex Alert" onClick="asAlert(’Hello World, from JavaScript’);" /></div>
</body>
相关文章推荐
- Flex中利用ExternalInterface的API调用JavaScript函数的例子
- Flex:JavaScript与ActionScript函数相互调用
- Flex反射:通过调用getDefintionByName函数动态创建按钮等控件的例子
- Flex中如何利用Matrix类的rotate函数对图片进行旋转操作的例子
- Flex中利用ExternalInterface API从JavaScript中获取内容并在Flex应用中使用的例子
- Flex应用中利用ExternalInterface API取得JavaScript返回值的例子
- Flex中如何利用htmlText和condenseWhite属性在RichTextEditor中紧缩HTML的空格的例子
- FLEX文本htmlText调用js函数或者发送flex事件
- Flex中如何利用mx.utils.UIDUtil类的createUID()函数创建UID的例子
- flex actionscript 和 javascript 通讯(2) javascript调用 actionscript的函数
- Flex中如何利用setInterval()和clearInterval()函数按照一定时间有规则的执行函数的例子
- Flex中如何利用Matrix类的rotate函数对图片进行旋转操作的例子
- Flex中如何利用FocusManager类的setFocus函数设置TextInput的焦点的例子
- Flex 中利用Matrix 类的rotate函数对图片进行旋转操作的例子
- Flex中利用Array类中的every()事件循环遍历一个数组的例子(ActionScript 3.0)
- Flex中如何利用dataDescriptor和labelFunction属性以及hasChildren()和getChildren()函数设置Tree中包含子节点个数的例子
- 对于qt5的信号机制的一点参考,然后利用模板实现:在新线程执行可变函数参数的调用
- Flex通过IFrame嵌入html,并调用html中的js脚本
- 利用VC调用动态链接库中的函数
- 如何利用Flex调用JavaScript代码