Javascript 调用 ActionScript 方法

1. 在Flex中,ActionScript调用Javascript是比较简单的,说白了就是,在html里,怎么调用Javascript,在ActionScript就怎么调用就可以了
2. 如果用js调用as,就稍微麻烦一点,其实也比较简单

MXML代码:
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="left" backgroundColor="white"
3    initialize="init()">
4
5    <mx:Label text="城市名称:"/>
6
7    <mx:List id="cityList" width="200" height="300" dataProvider="{cities}"/>
8
9    <mx:ArrayCollection id="cities">
10        <mx:String>北京</mx:String>
11        <mx:String>上海</mx:String>
12    </mx:ArrayCollection>
13   
14    <mx:Script>
15        <![CDATA[
16            private function init(): void
17            {
18                //注册回调函数供JavaScript调用
19                ExternalInterface.addCallback("callActionScript", asFunctionByJs);
20            }
21           
22            private function asFunctionByJs(city: String): void
23            {
24                cities.addItem(city);               
25            }
26        ]]>
27    </mx:Script>
28</mx:Application>

HTML代码(这些代码都是flex builder自动生成的,用于将flash嵌入到网页里,不用仔细看这些代码,注意黄色背景的部分,这是关键部分,是我加入到)

  1<!-- saved from url=(0014)about:internet -->
  2<html lang="en">
  3
  4<!-- 
  5Smart developers always View Source.
  6
  7This application was built using Adobe Flex, an open source framework
  8for building rich Internet applications that get delivered via the
  9Flash Player or to desktops via Adobe AIR.
10
11Learn more about Flex at http://flex.org
12// -->
13
14<head>
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16
17<!--  BEGIN Browser History required section -->
18<link rel="stylesheet" type="text/css" href="history/history.css" />
19<!--  END Browser History required section -->
20
21<title></title>
22<script src="AC_OETags.js" language="javascript"></script>
23
24<!--  BEGIN Browser History required section -->
25<script src="history/history.js" language="javascript"></script>
26<!--  END Browser History required section -->
27
28<style>
29body { margin: 0px; overflow:hidden }
30
</style>
31<script language="JavaScript" type="text/javascript">
32<!--
33// -----------------------------------------------------------------------------
34// Globals
35// Major version of Flash required
36var requiredMajorVersion = 9;
37// Minor version of Flash required
38var requiredMinorVersion = 0;
39// Minor version of Flash required
40var requiredRevision = 124;
41// -----------------------------------------------------------------------------
42// -->
43
</script>
44
45<script type="text/javascript">
46    function callActionScript(value)
47    {
48        //根据id获取flash实例,在这里id是CallAsFromJs,可以从Embed
49        var flash = (navigator.appName.indexOf ("Microsoft") !=-1)?window["CallAsFromJs"]:document["CallAsFromJs"];
50        //调用ActionScript注册的回调方法
51        flash.callActionScript(value);
52    }

53
</script>
54</head>
55
56<body scroll="no">
57输入城市名称:<input type="text" id="newCityName"/><input type="button" value="添加城市" onclick="callActionScript(newCityName.value);"/>
58<script language="JavaScript" type="text/javascript">
59<!--
60// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
61var hasProductInstall = DetectFlashVer(6, 0, 65);
62
63// Version check based upon the values defined in globals
64var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
65
66if ( hasProductInstall && !hasRequestedVersion ) {
67    // DO NOT MODIFY THE FOLLOWING FOUR LINES
68    // Location visited after installation is complete if installation is required
69    var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
70    var MMredirectURL = window.location;
71    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
72    var MMdoctitle = document.title;
73
74    AC_FL_RunContent(
75        "src", "playerProductInstall",
76        "FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
77        "width", "100%",
78        "height", "100%",
79        "align", "middle",
80        "id", "CallAsFromJs",
81        "quality", "high",
82        "bgcolor", "#ffffff",
83        "name", "CallAsFromJs",
84        "allowScriptAccess","sameDomain",
85        "type", "application/x-shockwave-flash",
86        "pluginspage", "http://www.adobe.com/go/getflashplayer"
87    );
88}
 else if (hasRequestedVersion) {
89    // if we've detected an acceptable version
90    // embed the Flash Content SWF when all tests are passed
91    AC_FL_RunContent(
92            "src", "CallAsFromJs",
93            "width", "100%",
94            "height", "100%",
95            "align", "middle",
96            "id", "CallAsFromJs",
97            "quality", "high",
98            "bgcolor", "#ffffff",
99            "name", "CallAsFromJs",
100            "allowScriptAccess","sameDomain",
101            "type", "application/x-shockwave-flash",
102            "pluginspage", "http://www.adobe.com/go/getflashplayer"
103    );
104  }
 else // flash is too old or we can't detect the plugin
105    var alternateContent = 'Alternate HTML content should be placed here. '
106      + 'This content requires the Adobe Flash Player. '
107       + '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
108    document.write(alternateContent);  // insert non-flash content
109  }

110// -->
111
</script>
112<noscript>
113      <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
114            id="CallAsFromJs" width="100%" height="100%"
115            codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
116            <param name="movie" value="CallAsFromJs.swf" />
117            <param name="quality" value="high" />
118            <param name="bgcolor" value="#ffffff" />
119            <param name="allowScriptAccess" value="sameDomain" />
120            <embed src="CallAsFromJs.swf" quality="high" bgcolor="#ffffff"
121                width="100%" height="100%" name="CallAsFromJs" align="middle"
122                play="true"
123                loop="false"
124                quality="high"
125                allowScriptAccess="sameDomain"
126                type="application/x-shockwave-flash"
127                pluginspage="http://www.adobe.com/go/getflashplayer">
128            </embed>
129    </object>
130</noscript>
131</body>
132</html>
133
总结,js调用as,大概分为3步:
  1. as使用ExternalInterface.addCallback注册回调函数
  2. 在js函数中根据flash在网页中的id获取实例
  3. 用上面获取到flash实例,调用as的函数
 
运行效果
由于需要单独编写一下页面才可以看到效果,不是简单的插入flash,所以,点击查看效果 
posted @ 2009-08-12 10:27  静候良机  阅读(5748)  评论(1编辑  收藏  举报