devxlogo

DHTML Loads Faster Than Accompanying HTML

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;       }   }

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.