Flex与JS通信

as js 通信 收藏

-----------asjsAlert.mxml---------------

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
 <mx:Script>
  <![CDATA[
   import flash.external.ExternalInterface;
   import mx.controls.Alert;
   //这里我们注册两个方法,一个是供AS调用JS
   //一个是供JS调用AS
   public function init():void
   {
    ExternalInterface.addCallback("asAlertText",asAlertText);
   }
   public function jsTest():void
   {
    //call定义调用JS的方法及参数
    ExternalInterface.call("jsAlertTest",jsTestText.text);
   }
   public function asAlertText(str:String):void
   {
    Alert.show(str,"test");
   }
  
  ]]>
 </mx:Script>
 <mx:Button x="58" y="43" label="Button" click="jsTest()"/>
 <mx:TextInput x="58" y="105" id="jsTestText"/>
</mx:Application>
 

--------asjsText.html-------------------

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="history/history.css" />
<title></title>
<script src="AC_OETags.js" language="javascript"></script>
<script src="history/history.js" language="javascript"></script>
<style>
body { margin: 0px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">
<!--
var requiredMajorVersion = 9;
var requiredMinorVersion = 0;
var requiredRevision = 124;
</script>
</head>

<body scroll="no">
<button onClick="asAlertTest()">JS调用AS方法</button>
<input type="text" id="asTestText" />

<script language="JavaScript" type="text/javascript">
<!--

function jsAlertTest(str)
{
alert(str);
}
function asAlertTest()
{
 thisMovie('asjsAlert').asAlertText(document.getElementById('asTestText').value);
}
function thisMovie(movieName)
{

   if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
   } else {
    return document[movieName];
   }
 }

var hasProductInstall = DetectFlashVer(6, 0, 65);
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

if ( hasProductInstall && !hasRequestedVersion ) {
 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", "asjsAlert",
  "quality", "high",
  "bgcolor", "#869ca7",
  "name", "asjsAlert",
  "allowScriptAccess","sameDomain",
  "type", "application/x-shockwave-flash",
  "pluginspage", "http://www.adobe.com/go/getflashplayer"
 );
} else if (hasRequestedVersion) {
 AC_FL_RunContent(
   "src", "asjsAlert",
   "width", "100%",
   "height", "100%",
   "align", "middle",
   "id", "asjsAlert",
   "quality", "high",
   "bgcolor", "#869ca7",
   "name", "asjsAlert",
   "allowScriptAccess","sameDomain",
   "type", "application/x-shockwave-flash",
   "pluginspage", "http://www.adobe.com/go/getflashplayer"
 );
  } else { 
    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); 
  }
</script>
<noscript>
   <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
   id="asjsAlert" width="100%" height="100%"
   codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
   <param name="movie" value="asjsAlert.swf" />
   <param name="quality" value="high" />
   <param name="bgcolor" value="#869ca7" />
   <param name="allowScriptAccess" value="sameDomain" />
   <embed src="asjsAlert.swf" quality="high" bgcolor="#869ca7"
    width="100%" height="100%" name="asjsAlert" align="center"
    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>

posted on 2010-05-14 16:36  念时  阅读(583)  评论(0编辑  收藏  举报

细节决定成败!态度决定一切!