JS代码:
<script>
function showMsg(msg){
alert(msg);
}
</script>
Applet代码:
public boolean callFromJS(String message) {
System.out.println(message);
}
1. 在Js中调用Applet的function
<script>
function callApplet(){
var appletObj;
var browserName=navigator.userAgent.toLowerCase();
if(/msie/i.test(browserName) && !/opera/.test(browserName)){
appletObj = document.applets[0];
}else if(/firefox/i.test(browserName)){
appletObj = document.applets[0];
}else if(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName)){
appletObj = document._applet;
}else{
appletObj = document.applets[0];
}
//Firefox
if(appletObj == null){
var appletEbd = document.embeds[0];
if(appletEbd != null){
appletEbd.callFromJS("hello");
}
}
//IE
else{
appletObj.callFromJS("hello");
}
}
</script>
2. 在Applet中调用JS的function
import netscape.javascript.JSObject //依赖plugin.jar
JSObject.getWindow(this).call("callFromJS", new String[]{"hello"});