简体   繁体   中英

deployJava.js adds an <embed> element to head area

I have a site where I am using Java applets and have included deployJava.js load tag in the head area of the page. However when I see resulting HTML in Chrome debugger this script breaks my head content starting body immediately, so other head content appears in body tag!

You can see it live on my site http://viva-games.ru/

Also deployJava inserts an embed tag (right after body opens) so you can see an empty line in the top of page.

What I am doing wrong?

deployJava.js is a pain to integrate since it uses document.write to insert tags in the page before the document is fully loaded. Basically, it doesn't support asynchronous loading.

According to the examples from Oracle , we have to include the file somewhere in the body and call it right after the inclusion. The applet tags will be placed there.

If you want to load it asynchronously using RequireJS for instance or if you want to call it later, after the page is loaded, download the uncompressed version and replace every occurrence of:

document.write(...);

by:

document.getElementsByTagName('body')[0].insertAdjacentHTML('afterbegin', ...);

Then you can call deployJava.runApplet anywhere in your JavaScript. You can even put the applet tags in a div element somewhere in your page. The HTML code:

<body>
    ...
    <div id="appletContainer"></div>

    // The include after the div or once the page is loaded
    <script type="text/javascript" src="javascript/deployJava.js"></script>
    ...
</body>

and the file deployJava.js :

document.getElementById('appletContainer').insertAdjacentHTML('afterbegin', ...);

Regarding the empty line, I don't have any explanation but a workaround can be found using CSS. If your applet doesn't show any GUI, you can set the appletContainer's height to 0.

NOTE: Don't hide the appletContainer using display:none , otherwise your applet won't run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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