一个js与as3交互的例子(Firefox下正常)

 js调用flash里面的方法,控制flash里面MC初始出现的X、Y坐标位置。

 

as3的代码:

Security.allowDomain("*");
var mc:MC;
var mcx:Number=200;
var mcy:Number=100;
txt.multiline = true;
txt.wordWrap = true;
txt.border = true;
txt.text = "Initializing...\n";
if (ExternalInterface.available) {
    try {
        txt.appendText("Adding callback...\n");
        ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
        if (checkJavaScriptReady()) {
            txt.appendText("JavaScript is ready.\n"+mcx+';'+mcy);
            mc = new MC();
            addChild(mc);
            mc.x = mcx;
            mc.y =mcy;
        } else {
            txt.appendText("JavaScript is not ready, creating timer.\n");
            var readyTimer:Timer = new Timer(100, 0);
            readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
            readyTimer.start();
        }
    } catch (error:SecurityError) {
        txt.appendText("A SecurityError occurred: " + error.message + "\n");
    } catch (error:Error) {
        txt.appendText("An Error occurred: " + error.message + "\n");
    }
} else {
    txt.appendText("External interface is not available for this container.");
}

function receivedFromJavaScript(val1:Number,val2:Number):void {
    mcx = val1;
    mcy = val2;
    txt.appendText("JavaScript is ready.\n"+mcx+';'+mcy);
}
function checkJavaScriptReady():Boolean {
    var isReady:Boolean = ExternalInterface.call("isReady");
    return isReady;
}
function timerHandler(event:TimerEvent):void {
    txt.appendText("Checking JavaScript status...\n");
    var isReady:Boolean = checkJavaScriptReady();
    if (isReady) {
        mc = new MC();
        addChild(mc);
        mc.x = mcx;
        mc.y =mcy;
        Timer(event.target).stop();
    }
}


html代码:

<html>
<head>
 <script language="JavaScript">
   
     var jsReady = false;
     function isReady() {
         return jsReady;
     }
     function pageInit() {
         jsReady = true;
         sendToActionScript(30,30);
     }
     function thisMovie(movieName) {
     
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document.embeds[movieName]; //document[movieName];
         }
     
     /*
     if (window.document[movieName])
       {
          return window.document[movieName];
       }
       if (navigator.appName.indexOf("Microsoft Internet")==-1)
       {
          if (document.embeds && document.embeds[movieName])
          return document.embeds[movieName];
       }
       else
       {
          return document.getElementById(movieName);
       }
       */
      
     }
     function sendToActionScript(val1,val2) {
         thisMovie("A").sendToActionScript(val1,val2);
     }
     function sendToJavaScript(value) {
         //document.forms["form1"].output.value += "ActionScript says: " + value + "\n";
     }
 
</script>
</head>
<body>
 <div id="t_divs1" style="position:absolute;width:600px;height:500px;top:50px;left:50px;">
 <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="600" height="500" id="A" name="A" align="center">
<param name="movie" value="A.swf" />
<param name="quality" value="medium" />
<param name="allowScriptAccess" value="sameDomain" />
<embed src="A.swf" quality="medium" width="600" height="500" name="A" id="A" align="center" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
 </div>
<!--
<input type="button" value="Send" onclick="sendToActionScript(200,200);" /><br />
-->
<script language="JavaScript">

 Ret = window.setTimeout(function(){
                    pageInit();
                }, 1000);               //此处延迟是为了解决firefox下js初始调用flash方法找不到的问题

</script>
   
</body>
</html>

你可能感兴趣的:(firefox)