How can I embed a Java applet dynamically with Javascript?

前端 未结 6 601
我寻月下人不归
我寻月下人不归 2021-01-02 03:13

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

6条回答
  •  死守一世寂寞
    2021-01-02 03:26

    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.

提交回复
热议问题