如何使用 Javascript 动态嵌入 Java 小程序?
我希望能够使用按下按钮时调用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将覆盖您的整个文档。如果您想保留文档,并且只想添加一个小程序,则需要附加它。
此代码将添加小程序作为 body 标记的最后一个元素。确保在 DOM 处理后运行它,否则您将收到错误。推荐使用 Body OnLoad 或 jQuery。
Will overwrite your entire document. If you want to keep the document, and just want an applet added, you'll need to append it.
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.
我会建议做一些像你正在做的事情;所以我很困惑为什么它不起作用。
这是一个文档< /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.
有一个用于此目的的 JavaScript 库:
http://www.java.com/js/deployJava.js
There is a JavaScript library for this purpose:
http://www.java.com/js/deployJava.js
我做了类似于 Beachhouse 建议的事情。我像这样修改了 deployJava.js:
它在 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:
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 itsinnerHTML
to theappletString
, but only IE were able to detect the new applet dynamically. Insert the wholediv
direclty to thebody
works on all browsers.创建一个新的 applet 元素并使用appendChild 将其附加到现有元素。
……
另外
,确保现有元素是可见的,也就是说你没有设置css来隐藏它,否则浏览器在使用appendChild后将无法加载小程序。我花了太多时间试图弄清楚这一点。
Create a new applet element and append it to an existing element using appendChild.
...
...
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.
这对我有用:
-----html代码:
This worked for me:
-----html code: