deployJava.js
presents challenges when it comes to integration due to its use of document.write
for inserting tags on the page before the document is fully loaded, lacking support for asynchronous loading.
As per examples provided by Oracle, we need to include the file somewhere in the body and immediately call it after inclusion for the applet tags to be correctly positioned.
If you wish to load it asynchronously using tools like RequireJS or want to delay the call until after the page has loaded, consider downloading the uncompressed version and replacing instances of:
document.write(...);
with:
document.getElementsByTagName('body')[0].insertAdjacentHTML('afterbegin', ...);
Subsequently, you can invoke deployJava.runApplet
at any point within your JavaScript code. Additionally, you have the freedom to place the applet tags inside a div
element located elsewhere on your page. Consider the following HTML setup:
<body>
...
<div id="appletContainer"></div>
// The script inclusion follows the div or once the page has finished loading
<script type="text/javascript" src="javascript/deployJava.js"></script>
...
</body>
Alongside the file deployJava.js
:
document.getElementById('appletContainer').insertAdjacentHTML('afterbegin', ...);
In regards to the empty line issue, an oddity without a clear explanation, CSS provides a workaround. If your applet does not require a visible GUI, setting the height of appletContainer to 0 resolves the problem.
NOTE: Avoid hiding the appletContainer using display:none
, as this will prevent your applet from functioning properly.