2016-10-13 00:16:48 点击量:30 标签: 收藏本文
演示实例(flash与js交互)Demo:
Flash提供了ExternalInterface类来负责和JS的交互,基本上的使用方法,在Flash帮助文档中有详细的说明,下边是Flash部分代码:
- //导入包
- import flash.external.*;
- //提供JS访问的函数名
- var _method:String = "_postStr";
- //指定本地函数中this变量的作用域,可设置为null留空
- var e_area:Object = null;
- //AS内部函数名
- var method:Function = postStr;
- //将函数注册到容器列表
- var wasSuccessful:Boolean = ExternalInterface.addCallback (_method, e_area, method);
- //查看注册是否成功
- if (wasSuccessful) {
- txtResult.text = "函数注册成功";
- } else {
- txtResult.text = "函数注册失败,请检查函数是否存在";
- }
- //本地函数,用以供JS调用
- function postStr (txt:String) {
- txttxtResult.text = txt;
- }
- //点击btn时,将txtInput.text的值传给js里的postStr函数
- btn.onPress = function () {
- ExternalInterface.call ("postStr", txtInput.text);
- };
JS的部分:
- <script>
- function postStr(string) {
- /*这部分是jquery的$.ajax代码,有需要的可以看下哈
- $.ajax({
- type:"POST",
- url:"rate.php",
- data:"name="+name,
- success:function(msg,textStatus){
- alert("string form as: \""+string+"\"");
- thisMovie("flash-js")._postStr("\""+string+"\" from js to as");
- return msg;
- }
- });
- */
- alert("string form as: \""+string+"\"");
- //把Flash传过来的值发送回到Flash里的postStr函数
- thisMovie("flash-js")._postStr("\""+string+"\" from js to as");
- }
- //浏览器兼容访问DOM
- function thisMovie(movieName) {
- if (navigator.appName.indexOf("Microsoft") != -1) {
- return window[movieName]
- }
- else {
- return document[movieName]
- }
- }
- </script>
来源:http://nwhy.org/flash-js-externalinterface.html
相关文章:Flash的as3,as2与JS的相互通信终极解密(含演示实例)
ExternalInterface.addCallback()方法,as2 和as3的区别
ExternalInterface.call和addCallback测试
使用externalInterface.addCallback()容易犯的两个错误
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室