Question:
I often have the problem in which the DHTML on my page loads faster than the accompanying HTML. Therefore, users are enticed to click away at elements, which trigger events that act on HTML that have not yet loaded. The result is a script error. I know I can trigger a function onload that continuously polls the readystate of the document. While it polls, ‘document.onmousedown = null’ works in nullifying mouse clicks. But how do I reset document.onmousedown to resume normal operation after the document’s readystate is true?
Answer:
You’re on the right path here, but rather than polling, you may want to just wrap your function bodies:
function mouseclickevent(){ if (document.readyState=="complete"){ // perform your processing. } }
You could also hold off adding the events to the objects until after the readystate is “complete”:
function document_onreadystatechange(){ if (document.readyState=="complete"){ // handle any final initializations myImage.onmousedown=mouseclickevent; } }