views:

432

answers:

3

Hello, I searched for it a lot but probably I am searching wrong strings. A Java applet is feeding live bits into my pages, java applet accesses the input fields on my page and places the information

<input type="hidden" id="F1" value="Nothing Yet">

and then it calls a javascript functionon the page say LivePicker() and then it simply picks up a value

var ClockVal = document.getElementById("F1").value;
document.getElementById("ICSCLOCK").innerHTML = ClockVal;

The problem I am facing is, this works fine but sometimes in firebug console it give errors like LivePicker is not defined, while LivePicker would be working perfectly fine on the page while sometimes it will give F1 is not defined, while my clock would be working fine. All of these errors appear at page load.

Java applet places the data sequentially, it first place the data and then calls the js function to process it. That works perfectly fine on test pages with minimum HTML and JS but when I integrate it to my application, which uses a lot of components from YUI and a lot of my own JS code (which is now minified obviously), it give these errors. One thing I would like to add, before minification, these errors were a lot likely but after minification of JS and CSS, the page load time is reduced to half and the appearence of these errors are reduced to half as well.

I am suspecting this is due to, on page load, applet tries to manipulate the DOM which is not ready yet. Is there anything, which could stop the applet to wait until the DOM is fully loaded? I tried window.onload and onDOMReady function of YUI, they seem to make no effect at all.

Can any one help please?

A: 

The browser should delay executing the window.onload code until after the DOM tree is created and after all other external resources are fully loaded and the page is displayed in the browser.

The window.onload should work. Your applet must be running before the onload event.

As a test, can you do the following and see if it changes anything:

  • create a hidden "set" field in the page with value "false";
  • on window.onload set the value for the "set" field to "true";
  • in your applet check the "set" field;
  • only start doing things in your applet if the "set" field is defined and has a value of "true";

One other thing, don't do a busy waiting in your applet to test for the "set" field. Your applet should take no action if the field is undefined or false. You can reactivate it on window.onload if needed.

dpb
Thanks for your detailed reply. I am trying to see what happen if I do that but just a quick question, this hidden input element will also be in DOM, what if it hasn't get loaded and applet tries to load it.
Hammad Tariq
I don’t remember exactly what happens in that case; you either get an exception or you get null instead, both cases meaning that the field is not defined. Another thing you could try (which I think should be simpler) would be to let the applet load and do nothing. Then, on window.onload have a javascript function in your page that calls a method on your applet to start it.
dpb
I tried that, it seems to be some other problem within applets and I am suspecting that child threads are not getting terminated. I have scaled down the code, now going step by step to find the problem in applets. Please see my other thread http://stackoverflow.com/questions/2134028/terminating-java-applet-thread-on-page-unloadIf you can help me in that.
Hammad Tariq
+1  A: 

you could try using setTimeout to delay execution

https://developer.mozilla.org/en/DOM/window.setTimeout

or the jQuery library could also help out with the ready event on the document object

http://docs.jquery.com/Events/ready#fn

PeanutPower
A: 

This problem was resolved using dpb suggestions in slightly different way. This blog post I wrote should explain every bit to those who are facing similar problem.

Controlling Java applet from Javascript especially if you make round-trips to the web page from applet

Hammad Tariq