如何使用 Javascript 动态嵌入 Java 小程序? [英] How can I embed a Java applet dynamically with Javascript?

查看:21
本文介绍了如何使用 Javascript 动态嵌入 Java 小程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

推荐答案

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);

此代码将小程序添加为 body 标记的最后一个元素.确保在 DOM 处理完之后运行它,否则你会得到一个错误.推荐使用 Body OnLoad 或 jQuery ready.

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.

这篇关于如何使用 Javascript 动态嵌入 Java 小程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆