Actionscript 部分:
stop();
this.createTextField("txt",0,0,0,100,50);
txt.multiline = true;
txt.text = "Hello World!";
function receivedMessageFromJavasscript(myMessage:String):Void {
txt.text = myMessage;
}
flash.external.ExternalInterface.addCallback("displayMessage", this, receivedMessageFromJavasscript);
Javascript 部分:
var getFlashMovieObject = function(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);
}
}
var sendMessageToFlash = function(){
var myMessage = document.getElementById("message").value;
var flashObject = getFlashMovieObject("flash");
flashObject.displayMessage(myMessage);
}
完整 HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Actionscript Javascript Communication</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
var getFlashMovieObject = function(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);
}
}
var sendMessageToFlash = function(){
var myMessage = document.getElementById("message").value;
var flashObject = getFlashMovieObject("flash");
flashObject.displayMessage(myMessage);
}
</script>
</head>
<body>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 'codebase',
'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
id="flash" align="middle">
<param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="CallFromJS.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="CallFromJS.swf" quality="high" bgcolor="#ffffff" width="200" height="200" name="flash" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<p>
<input id="message" type="text" value="Message"/>
</p>
<p>
<button onclick="sendMessageToFlash()">
Send
</button>
</p>
</body>
</html>
Javascript call Actionscript 的 function:
Actionscript 部分:
stop();
//Create input text field
this.createTextField("txt",0,0,0,100,50);
txt.multiline = true;
txt.type = "input";
txt.text = "Enter Message";
//Create button
this.createEmptyMovieClip("myButton1", this.getNextHighestDepth());
myButton1._x = 50;
myButton1._y = 100;
myButton1.createEmptyMovieClip("buttonBkg", myButton1.getNextHighestDepth());
myButton1.buttonBkg.lineStyle(0, 0x820F26, 60, true, "none", "square", "round");
myButton1.buttonBkg.lineTo(100, 0);
myButton1.buttonBkg.lineTo(100, 30);
myButton1.buttonBkg.lineTo(0, 30);
myButton1.buttonBkg.lineTo(0, 0);
myButton1.createTextField("btnValue", 1, 25, 10, 75, 25);
myButton1.btnValue.text = "Send To JS";
myButton1.onPress = sendMessageToJS;
function sendMessageToJS():Void{
flash.external.ExternalInterface.call("CallFromFlash", txt.text);
}
Javascript 部分:
var CallFromFlash = function(message){
document.getElementById("message").value = message;
}
完整 HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Actionscript Javascript Communication</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
var CallFromFlash = function(message){
document.getElementById("message").value = message;
}
</script>
</head>
<body>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 'codebase',
'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
id="flash" align="middle">
<param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="CallJS.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="CallJS.swf" quality="high" bgcolor="#ffffff" width="200" height="200" name="flash" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<p>
<input id="message" type="text" value="Message"/>
</p>
</body>
</html>
相關書籍: