我正在尝试运行一个小程序的小测试来调用 JavaScript 方法,反之亦然。该代码在 IE 9.0 和 Firefox 17.0 中运行良好,但在 Chrome 23.0.1271.91 m 中无法运行。
我不确定这是否是这个特定版本的 Chrome 的问题,但我想不是。
基本上,我有一个主 HTML 页面,它使用“对象”标签和“iframe”标签声明小程序。在另一个文件中,我有一个 HTML 页面,该页面将从主页面加载到 iframe 中,但由小程序控制。
在 main.html 文件中,我使用“object”标签而不是“applet”来符合 HTML5。我知道这个测试应该使用“applet”标签,但使用“applet”标签正是我想要避免的。
小程序代码:
package tests.javaplugin;
import java.applet.Applet;
import java.util.*;
import netscape.javascript.JSObject;
public class PluginTestApplet extends Applet {
private static final long serialVersionUID = 492358995598940846L;
private static Timer theTimer = new Timer(true);
private static JSObject theWindow;
@Override
public void init() {
System.out.println("entering init()");
theWindow = JSObject.getWindow(this);
System.out.println("exiting init()");
}
public void loadComplete() {
System.out.println("calling loadComplete()");
theTimer.schedule(new ShowPageTimerTask(), 100);
System.out.println("returning loadComplete()");
}
public int getRandomInteger() {
System.out.println("calling getRandomInteger()");
final int random = (int) Math.abs(Math.random() * 100);
System.out.println("returning getRandomInteger(): " + random);
return random;
}
private class ShowPageTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("entering ShowPageTimerTask.run()");
theWindow.call("showPage", new String[] { "./page.html" });
System.out.println("exiting ShowPageTimerTask.run()");
}
}
}
main.html 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1252">
<title>Testing Java Plug-in in Java 7.0</title>
<script src="./mainControl.js"></script>
</head>
<body onload="onBodyLoad()">
<!--[if !IE]>-->
<object id="applet_embed" width="0" height="0"
type="application/x-java-applet;version=1.7">
<!--<![endif]-->
<!--[if IE]>
<object id="applet_embed" width="0" height="0"
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
<![endif]-->
<param name="archive" value="PluginTestApplet.jar">
<param name="codebase" value="./">
<param name="code" value="tests.javaplugin.PluginTestApplet">
<param name="scriptable" value="true">
<param name="mayscript" value="true">
No Java 2 SDK, Standard Edition v 1.7 support for APPLET!
</object>
<div id="principalDIV" style="position: absolute; width: 640px; height: 480px; top: 0px; left: 0px; scrolling: no; visibility: visible; z-index: 0; overflow: hidden; leftmargin=0px; topmargin=0px; background-color: #000000">
<iframe id="ifPrincipal" style="position: absolute; width: 100%; height: 100%; visibility: visible; scrolling: no; overflow-x: hidden; overflow-y: hidden;">
</iframe>
</div>
</body>
</html>
mainControl.js 代码:
function getApplet() {
return document.getElementById("applet_embed");
}
function onBodyLoad() {
getApplet().loadComplete();
}
function getRandomInteger() {
return getApplet().getRandomInteger();
}
function showPage(aURL) {
var iframePrincipal = document.getElementById("ifPrincipal");
if (iframePrincipal.src != null) {
iframePrincipal.src = aURL;
} else {
alert("Error! iframe src = null! " + iframePrincipal.src);
}
}
page.html 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1252">
<title>Testing Java Plug-in in Java 7.0</title>
<script src="./control.js"></script>
</head>
<body onload="onBodyLoad()" style="background-color: #000000; color: #ffffff; font-family: Tahoma; font-size: 35px;" >
<div>Testing dynamic HTML i-frame commanded by Java Plug-in.</div>
<div style="float: left;">The random number is </div><div id="randomNum" style="float: left;"> </div>
</body>
</html>
control.js 代码:
function onBodyLoad() {
var randomNumber = parent.getRandomInteger();
var randomNumberDiv = document.getElementById("randomNum");
randomNumberDiv.innerHTML = randomNumber;
}
控制台(在开发工具中)显示以下错误:
Uncaught TypeError: Object #<HTMLObjectElement> has no method 'loadComplete' mainControl.js:6
Failed to load resource file:///C:/Projects/Tests/javaplugintest/tests.javaplugin.PluginTestApplet
那么,Chrome 需要在 'object' 标签声明中有什么技巧或特定参数吗?