简体   繁体   English

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

[英]How can I embed a Java applet dynamically with Javascript?

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. 我希望能够使用按下按钮时调用的Javascript函数动态地将Java小程序插入到网页中。 (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. (在页面加载时加载applet会使事情变得过于缓慢,冻结浏览器等等...)我使用以下代码,它在FF中无缝运行,但在IE8,Safari 4和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? 有没有人知道为什么这不能按预期工作,以及如何以适用于所有浏览器的方式动态插入applet? 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. 我已经尝试过使用其他地方建议的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);
}
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. 如果您想保留文档,只想添加一个applet,则需要附加它。

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. 此代码将applet添加为body标记的最后一个元素。 Make sure this is run after the DOM has processed or you will get an error. 确保在DOM处理完毕后运行,否则您将收到错误。 Body OnLoad, or jQuery ready recommended. Body OnLoad,或jQuery ready。

There is a JavaScript library for this purpose: http://www.java.com/js/deployJava.js 为此目的有一个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>

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. 但也许applet / object标签有一些神奇的东西,如果动态插入它们就不会被处理。 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? 没有更合格的建议,我有一个疯狂的解决方法为您提供:Howzabout您将applet呈现在不同的页面上,并动态创建一个IFRAME以在您的applet占据的空间中显示该页面? IFRAMEs are a bit more consistent in syntax across browsers, and I'd be surprised if they were to fail the same way. IFRAME在不同浏览器的语法上更加一致,如果它们以同样的方式失败,我会感到惊讶。

Maybe you should use your browser's debugging tools to look at the DOM after you swap in your applet node. 也许您应该使用浏览器的调试工具在交换applet节点后查看DOM。 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. 您的代码看起来不错,但我对动态applet不是很有经验。

I did something similar to what Beachhouse suggested. 我做了类似于Beachhouse建议的事情。 I modified the deployJava.js like this: 我像这样修改了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);
}

It worked ok on Chrome, Firefox and IE. 它在Chrome,Firefox和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. 我首先尝试在我的html文件上创建一个div并将其innerHTML设置为appletString ,但只有IE能够动态检测新的applet。 Insert the whole div direclty to the body works on all browsers. 将整个div direclty插入到body适用于所有浏览器。

Create a new applet element and append it to an existing element using appendChild. 创建一个新的applet元素,并使用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. 此外,确保现有元素可见,这意味着您没有设置css来隐藏它,否则浏览器将不会在使用appendChild后加载applet。 I spent too many hours trying to figure that out. 我花了太多时间试图解决这个问题。

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: ----- HTML代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM