如何使用 Javascript 动态嵌入 Java 小程序?

发布于 2024-09-10 01:53:14 字数 1040 浏览 3 评论 0原文

我希望能够使用按下按钮时调用的 Javascript 函数动态地将 Java 小程序插入网页中。 (在页面加载时加载小程序会减慢速度太多,冻结浏览器等...)我正在使用以下代码,它在 FF 中无缝工作,但在 IE8、Safari 4 和 Chrome 中失败且没有错误消息。有谁知道为什么这不能按预期工作,以及如何以适用于所有浏览器的方式动态插入小程序?我已经尝试按照其他地方的建议使用 document.write() ,但是在页面加载后调用该方法会导致页面被删除,所以这对我来说不是一个选择。

function createPlayer(parentElem)
{
    // The abc variable is declared and set here

    player = document.createElement('object');
    player.setAttribute("classid", "java:TunePlayer.class");
    player.setAttribute("archive", "TunePlayer.class,PlayerListener.class,abc4j.jar");
    player.setAttribute("codeType", "application/x-java-applet");
    player.id = "tuneplayer";
    player.setAttribute("width", 1);
    player.setAttribute("height", 1);

    param = document.createElement('param');
    param.name = "abc";
    param.value = abc;
    player.appendChild(param);

    param = document.createElement('param');
    param.name = "mayscript";
    param.value = true;
    player.appendChild(param);

    parentElem.appendChild(player);
}

I want to be able to insert a Java applet into a web page dynamically using a Javascript function that is called when a button is pressed. (Loading the applet on page load slows things down too much, freezes the browser, etc...) I am using the following code, which works seamlessly in FF, but fails without error messages in IE8, Safari 4, and Chrome. Does anyone have any idea why this doesn't work as expected, and how to dynamically insert an applet in a way that works in all browsers? I've tried using document.write() as suggested elsewhere, but calling that after the page has loaded results in the page being erased, so that isn't an option for me.

function createPlayer(parentElem)
{
    // The abc variable is declared and set here

    player = document.createElement('object');
    player.setAttribute("classid", "java:TunePlayer.class");
    player.setAttribute("archive", "TunePlayer.class,PlayerListener.class,abc4j.jar");
    player.setAttribute("codeType", "application/x-java-applet");
    player.id = "tuneplayer";
    player.setAttribute("width", 1);
    player.setAttribute("height", 1);

    param = document.createElement('param');
    param.name = "abc";
    param.value = abc;
    player.appendChild(param);

    param = document.createElement('param');
    param.name = "mayscript";
    param.value = true;
    player.appendChild(param);

    parentElem.appendChild(player);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

本王不退位尔等都是臣 2024-09-17 01:53:14
document.write()

将覆盖您的整个文档。如果您想保留文档,并且只想添加一个小程序,则需要附加它。

var app = document.createElement('applet');
app.id= 'Java';
app.archive= 'Java.jar';
app.code= 'Java.class';
app.width = '400';
app.height = '10';
document.getElementsByTagName('body')[0].appendChild(app);

此代码将添加小程序作为 body 标记的最后一个元素。确保在 DOM 处理后运行它,否则您将收到错误。推荐使用 Body OnLoad 或 jQuery。

document.write()

Will overwrite your entire document. If you want to keep the document, and just want an applet added, you'll need to append it.

var app = document.createElement('applet');
app.id= 'Java';
app.archive= 'Java.jar';
app.code= 'Java.class';
app.width = '400';
app.height = '10';
document.getElementsByTagName('body')[0].appendChild(app);

This code will add the applet as the last element of the body tag. Make sure this is run after the DOM has processed or you will get an error. Body OnLoad, or jQuery ready recommended.

潇烟暮雨 2024-09-17 01:53:14

我会建议做一些像你正在做的事情;所以我很困惑为什么它不起作用。

这是一个文档< /a> 这看起来相当权威,可以说是出自马嘴。它提到了不同浏览器的特性。您最终可能需要为不同的实现执行不同的标签汤。

但也许小程序/对象标签有一些神奇之处,如果动态插入,它们将无法被处理。由于没有更多合格的建议,我有一个疯狂的解决方法可以为您提供:您如何在不同的页面上显示小程序,并动态创建一个 IFRAME 来在您的小程序应该占用的空间中显示该页面? IFRAME 在跨浏览器的语法上更加一致,如果它们以同样的方式失败,我会感到惊讶。

也许您应该在交换小程序节点后使用浏览器的调试工具来查看 DOM。也许它没有出现在您认为的位置,或者没有按照您认为正在创建的结构出现。您的代码对我来说看起来不错,但我对动态小程序不太有经验。

I would have suggested doing something like what you're doing; so I'm baffled as to why it's not working.

Here's a document that looks pretty authoritative, coming from the horse's mouth as it were. It mentions the idiosyncrasies of different browsers. You may end up needing to do different tag soups for different implementations.

But maybe there's something magic about applet/object tags that keeps them from being processed if inserted dynamically. Having no more qualified advice, I have a crazy workaround to offer you: Howzabout you present the applet on a different page, and dynamically create an IFRAME to show that page in the space your applet should occupy? IFRAMEs are a bit more consistent in syntax across browsers, and I'd be surprised if they were to fail the same way.

Maybe you should use your browser's debugging tools to look at the DOM after you swap in your applet node. Maybe it's not appearing where you think it is, or not with the structure you think you're creating. Your code looks OK to me but I'm not very experienced with dynamic applets.

小霸王臭丫头 2024-09-17 01:53:14

有一个用于此目的的 JavaScript 库:
http://www.java.com/js/deployJava.js

// launch the Java 2D applet on JRE version 1.6.0
// or higher with one parameter (fontSize)
<script src=
    "http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = {code:'java2d.Java2DemoApplet.class',
        archive:'http://java.sun.com/products/plugin/1.5.0/demos/plugin/jfc/Java2D/Java2Demo.jar',
        width:710, height:540} ;
    var parameters = {fontSize:16} ;
    var version = '1.6' ;
    deployJava.runApplet(attributes, parameters, version);
</script>

There is a JavaScript library for this purpose:
http://www.java.com/js/deployJava.js

// launch the Java 2D applet on JRE version 1.6.0
// or higher with one parameter (fontSize)
<script src=
    "http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = {code:'java2d.Java2DemoApplet.class',
        archive:'http://java.sun.com/products/plugin/1.5.0/demos/plugin/jfc/Java2D/Java2Demo.jar',
        width:710, height:540} ;
    var parameters = {fontSize:16} ;
    var version = '1.6' ;
    deployJava.runApplet(attributes, parameters, version);
</script>
霊感 2024-09-17 01:53:14

我做了类似于 Beachhouse 建议的事情。我像这样修改了 deployJava.js

writeAppletTag: function(attributes, parameters) {
    ...

    // don't write directly to document anymore
    //document.write(startApplet + '\n' + params + '\n' + endApplet);

    var appletString = startApplet + '\n' + params + '\n' + endApplet;
    var divApplet = document.createElement('div');

    divApplet.id = "divApplet";
    divApplet.innerHTML = appletString;
    divApplet.style = "visibility: hidden; display: none;";

    document.body.appendChild(divApplet);
}

它在 Chrome、Firefox 和 IE 上运行正常。到目前为止没有问题。

我首先尝试在我的 html 文件上创建一个 div 并将其 innerHTML 设置为 appletString,但只有 IE 能够动态检测新的小程序。将整个 div 直接插入 body 适用于所有浏览器。

I did something similar to what Beachhouse suggested. I modified the deployJava.js like this:

writeAppletTag: function(attributes, parameters) {
    ...

    // don't write directly to document anymore
    //document.write(startApplet + '\n' + params + '\n' + endApplet);

    var appletString = startApplet + '\n' + params + '\n' + endApplet;
    var divApplet = document.createElement('div');

    divApplet.id = "divApplet";
    divApplet.innerHTML = appletString;
    divApplet.style = "visibility: hidden; display: none;";

    document.body.appendChild(divApplet);
}

It worked ok on Chrome, Firefox and IE. No problems so far.

I tried at first to have a div already created on my html file and just set its innerHTML to the appletString, but only IE were able to detect the new applet dynamically. Insert the whole div direclty to the body works on all browsers.

内心激荡 2024-09-17 01:53:14

创建一个新的 applet 元素并使用appendChild 将其附加到现有元素。

var applet = document.createElement('applet');
applet.id = 'player';

……

var param = document.createElement('param');

另外

applet.appendChild(param);
document.getElementById('existingElement').appendChild(applet);

,确保现有元素是可见的,也就是说你没有设置css来隐藏它,否则浏览器在使用appendChild后将无法加载小程序。我花了太多时间试图弄清楚这一点。

Create a new applet element and append it to an existing element using appendChild.

var applet = document.createElement('applet');
applet.id = 'player';

...

var param = document.createElement('param');

...

applet.appendChild(param);
document.getElementById('existingElement').appendChild(applet);

Also, make sure the existing element is visible, meaning you haven't set css to hide it, otherwise the browser will not load the applet after using appendChild. I spent too many hours trying to figure that out.

怪异←思 2024-09-17 01:53:14

这对我有用:

// my js code
    var app = document.createElement('applet');
    app.code= 'MyApplet2.class';
    app.width = '400';
    app.height = '10';

    var p1 = document.createElement('param');
    p1.name = 'sm_UnwindType';
    p1.value='200';
    var p2 = document.createElement('param');
    p2.name = 'sm_Intraday';
    p2.value='300';

    app.appendChild(p1);
    app.appendChild(p2);

    var appDiv = document.getElementById('applet_div');
    appDiv.appendChild(app);

-----html代码:

<div id="applet_div"></div>

This worked for me:

// my js code
    var app = document.createElement('applet');
    app.code= 'MyApplet2.class';
    app.width = '400';
    app.height = '10';

    var p1 = document.createElement('param');
    p1.name = 'sm_UnwindType';
    p1.value='200';
    var p2 = document.createElement('param');
    p2.name = 'sm_Intraday';
    p2.value='300';

    app.appendChild(p1);
    app.appendChild(p2);

    var appDiv = document.getElementById('applet_div');
    appDiv.appendChild(app);

-----html code:

<div id="applet_div"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文