Javascript 調(diào)用 ActionScript 的簡(jiǎn)單方法
1. 在Flex中,ActionScript調(diào)用Javascript是比較簡(jiǎn)單的,說(shuō)白了就是,在html里,怎么調(diào)用Javascript,在ActionScript就怎么調(diào)用就可以了
2. 如果用js調(diào)用as,就稍微麻煩一點(diǎn),其實(shí)也比較簡(jiǎn)單
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="城市名稱(chēng):"/>
<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
{
//注冊(cè)回調(diào)函數(shù)供JavaScript調(diào)用
ExternalInterface.addCallback("callActionScript", asFunctionByJs);
}
private function asFunctionByJs(city: String): void
{
cities.addItem(city);
}
]]>
</mx:Script>
</mx:Application>
HTML代碼(這些代碼都是flex builder自動(dòng)生成的,用于將flash嵌入到網(wǎng)頁(yè)里,不用仔細(xì)看這些代碼,注意黃色背景的部分,這是關(guān)鍵部分,是我加入到)
<!-- 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)
{
//根據(jù)id獲取flash實(shí)例,在這里id是CallAsFromJs,可以從Embed
var flash = (navigator.appName.indexOf ("Microsoft") !=-)?window["CallAsFromJs"]:document["CallAsFromJs"];
//調(diào)用ActionScript注冊(cè)的回調(diào)方法
flash.callActionScript(value);
}
</script>
</head>
<body scroll="no">
輸入城市名稱(chēng):<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>
總結(jié),js調(diào)用as,大概分為3步:
1.as使用ExternalInterface.addCallback注冊(cè)回調(diào)函數(shù)
2.在js函數(shù)中根據(jù)flash在網(wǎng)頁(yè)中的id獲取實(shí)例
3.用上面獲取到flash實(shí)例,調(diào)用as的函數(shù)
以上所述是小編給大家介紹的Javascript 調(diào)用 ActionScript 的簡(jiǎn)單方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JS實(shí)現(xiàn)網(wǎng)頁(yè)導(dǎo)航條特效
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)網(wǎng)頁(yè)導(dǎo)航條特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
setTimeout自動(dòng)觸發(fā)一個(gè)js的方法
本文為大家介紹下使用setTimeout自動(dòng)觸發(fā)一個(gè)js,具體實(shí)現(xiàn)如下,喜歡的朋友可以學(xué)習(xí)下2014-01-01
JS觸發(fā)事件event.target VS event.currentTarget實(shí)例
這篇文章主要介紹了JS觸發(fā)事件event.target VS event.currentTarget實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
JavaScript設(shè)置獲取和設(shè)置屬性的方法
這篇文章主要介紹了JavaScript設(shè)置獲取和設(shè)置屬性的方法,學(xué)會(huì)使用getAttribute、setAttribute的用法,需要的朋友可以參考下2015-03-03
JavaScript中的document.querySelector()方法使用詳解
HTML的DOM querySelector()方法可以不需要額外的jQuery等支持,也可以方便的獲取DOM元素,語(yǔ)法跟jQuery類(lèi)似,這篇文章主要給大家介紹了關(guān)于JavaScript中document.querySelector()方法使用的相關(guān)資料,需要的朋友可以參考下2024-06-06
JS 實(shí)現(xiàn)BASE64_ENCODE和BASE64_DECODE(實(shí)例代碼)
JS 實(shí)現(xiàn)BASE64_ENCODE和BASE64_DECODE(實(shí)例代碼)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
js實(shí)時(shí)獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼
在網(wǎng)頁(yè)中實(shí)時(shí)的顯示時(shí)間,不但可以給網(wǎng)頁(yè)添色,還可以方便瀏覽者掌握當(dāng)前時(shí)間,為了提高網(wǎng)站的開(kāi)發(fā)速度,可以把主代碼封裝在一個(gè)單獨(dú)的函數(shù)里面,在需要的時(shí)候直接調(diào)用 而我為了演示,直接寫(xiě)在了主頁(yè)面,方便大家觀(guān)看2013-06-06

