首先下载swfobject.js(http://code.google.com/p/swfobject/downloads/list)
可以使用FlashDevelop编译as文件(注:编译时选择Release,不要选择Debug,因为Debug会把一些没用的文件编译进swf文件)
as3 code:
package
{
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.external.ExternalInterface;
public class Main extends Sprite
{
var str:String = "'js call flash function and flash call js function!'";
public function Main() {
try {
ExternalInterface.addCallback("flAlert", this.showAlert);
} catch (ex:Error) {
return;
}
}
public function showAlert() {
ExternalInterface.call("jsAlert("+str+")");
}
}
}
html code:
<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF("helloworld.swf", "myContent", "300", "120", "9.0.0");
function jsAlert(v) {
alert(v);
}
function callExternal() {
document.getElementById("myContent").flAlert();
}
</script>
</head>
<body>
<input type="button" onClick="callExternal()" value="Call Flash">
<div id="myContent">
<p>Alternative content</p>
</div>
</body>
</html>