我正在使用 JSObject 从我的 Applet 调用 Javascript 函数。
我的小程序的 SSCCE:
package ch.vrag.web;
import java.applet.Applet;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import netscape.javascript.JSObject;
public class Sscce extends Applet {
private static final long serialVersionUID = -5403444704039831864L;
public void init() {
try {
Policy.setPolicy(new Policy() {
Permissions perms = new Permissions();
public PermissionCollection getPermissions(CodeSource codesource) throws SecurityException {
AllPermission grant = null;
grant = new AllPermission();
perms.add(grant);
return perms;
}
public void refresh() {
// Do nothing
}
});
}
catch (Exception e) {
}
JSObject window = JSObject.getWindow(this);
JSObject phoneNums = (JSObject) window.eval("getWindowRectangle()");
int x;
int y;
int width;
int height;
if (phoneNums.getSlot(0) instanceof Integer) {
x = (Integer) phoneNums.getSlot(0);
y = (Integer) phoneNums.getSlot(1);
width = (Integer) phoneNums.getSlot(2);
height = (Integer) phoneNums.getSlot(3);
}
else if (phoneNums.getSlot(0) instanceof Double) {
x = ((Double) phoneNums.getSlot(0)).intValue();
y = ((Double) phoneNums.getSlot(1)).intValue();
width = ((Double) phoneNums.getSlot(2)).intValue();
height = ((Double) phoneNums.getSlot(3)).intValue();
}
else {
return;
}
System.out.println("x: " + x + " y: " + y + " width: " + width + " height: " + height);
};
}
我的 HTML 文件的 SSCCE:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
function getWindowRectangle(){
var winX = (document.all)?window.screenLeft-2:window.screenX;
var winY = (document.all)?window.screenTop-2:window.screenY;
var winWidth = (document.all)?document.documentElement.offsetWidth+4:window.outerWidth;
var winHeight = (document.all)?document.documentElement.offsetHeight+4:window.outerHeight;
return([winX,winY,winWidth,winHeight]);
}
</script>
<title>SSCCE</title>
</head>
<body>
this is an SSCCE
<APPLET CODE="ch.vrag.web.Sscce.class" name="SSCCE" codebase="scripts" archive="Sscce.jar" width=0 height=0 MAYSCRIPT >
</APPLET>
</body>
这在 Internet Explorer 6 中效果很好。我有这个 Java 异常:
java.lang.Exception: netscape.javascript.JSException: Unknown Name
这个异常有什么用?我究竟做错了什么?