DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> Javascript 調用 ActionScript 的簡單方法
Javascript 調用 ActionScript 的簡單方法
編輯:關於JavaScript     

1. 在Flex中,ActionScript調用Javascript是比較簡單的,說白了就是,在html裡,怎麼調用Javascript,在ActionScript就怎麼調用就可以了

2. 如果用js調用as,就稍微麻煩一點,其實也比較簡單

MXML代碼:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com//mxml" layout="vertical" horizontalAlign="left" backgroundColor="white"
initialize="init()">
<mx:Label text="城市名稱:"/>
<mx:List id="cityList" width="" height="" dataProvider="{cities}"/>
<mx:ArrayCollection id="cities">
<mx:String>北京</mx:String>
<mx:String>上海</mx:String>
</mx:ArrayCollection>
<mx:Script>
<![CDATA[
private function init(): void
{
//注冊回調函數供JavaScript調用
ExternalInterface.addCallback("callActionScript", asFunctionByJs);
}
private function asFunctionByJs(city: String): void
{
cities.addItem(city); 
}
]]>
</mx:Script>
</mx:Application>

HTML代碼(這些代碼都是flex builder自動生成的,用於將flash嵌入到網頁裡,不用仔細看這些代碼,注意黃色背景的部分,這是關鍵部分,是我加入到)

<!-- saved from url=(0014)about:internet -->
<html lang="en">
<!-- 
Smart developers always View Source. 
This application was built using Adobe Flex, an open source framework
for building rich Internet applications that get delivered via the
Flash Player or to desktops via Adobe AIR. 
Learn more about Flex at http://flex.org 
// -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-" />
<!-- BEGIN Browser History required section -->
<link rel="stylesheet" type="text/css" href="history/history.css" />
<!-- END Browser History required section -->
<title></title>
<script src="AC_OETags.js" language="javascript"></script>
<!-- BEGIN Browser History required section -->
<script src="history/history.js" language="javascript"></script>
<!-- END Browser History required section -->
<style>
body {}{ margin: px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required

var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 124;
// -----------------------------------------------------------------------------
// -->
</script>
<script type="text/javascript">
function callActionScript(value)
{
//根據id獲取flash實例,在這裡id是CallAsFromJs,可以從Embed
var flash = (navigator.appName.indexOf ("Microsoft") !=-)?window["CallAsFromJs"]:document["CallAsFromJs"];
//調用ActionScript注冊的回調方法
flash.callActionScript(value);
}
</script>
</head>
<body scroll="no">
輸入城市名稱:<input type="text" id="newCityName"/><input type="button" value="添加城市" onclick="callActionScript(newCityName.value);"/>
<script language="JavaScript" type="text/javascript">
<!--
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)

var hasProductInstall = DetectFlashVer(6, 0, 65);
// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if ( hasProductInstall && !hasRequestedVersion ) {
// DO NOT MODIFY THE FOLLOWING FOUR LINES
// Location visited after installation is complete if installation is required
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
var MMredirectURL = window.location;
document.title = document.title.slice(0, 47) + " - Flash Player Installation";
var MMdoctitle = document.title;
AC_FL_RunContent(
"src", "playerProductInstall",
"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "CallAsFromJs",
"quality", "high",
"bgcolor", "#ffffff",
"name", "CallAsFromJs",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "CallAsFromJs",
"width", "%",
"height", "%",
"align", "middle",
"id", "CallAsFromJs",
"quality", "high",
"bgcolor", "#ffffff",
"name", "CallAsFromJs",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else { // flash is too old or we can't detect the plugin
var alternateContent = 'Alternate HTML content should be placed here. '
+ 'This content requires the Adobe Flash Player. '
+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
document.write(alternateContent); // insert non-flash content
}
// -->
</script>
<noscript>
<object classid="clsid:DCDBE-AED-cf-B-"
id="CallAsFromJs" width="%" height="%"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="CallAsFromJs.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="allowScriptAccess" value="sameDomain" />
<embed src="CallAsFromJs.swf" quality="high" bgcolor="#ffffff"
width="%" height="%" name="CallAsFromJs" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
</noscript>
</body>
</html>

總結,js調用as,大概分為3步:

1.as使用ExternalInterface.addCallback注冊回調函數

2.在js函數中根據flash在網頁中的id獲取實例

3.用上面獲取到flash實例,調用as的函數

以上所述是小編給大家介紹的Javascript 調用 ActionScript 的簡單方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved