0

While I do not know if it is possible, I am always facing such scenario for a long time without a "good" way to solve it. No matter what technology I used: ASP.NET, plaing JS/JQuery, any other web development frameworks...

The scenario is that:

How to set up a callback function which will fired when some specific DOM element is rendered?

For example, I have a button on the document, and I would like to simulate a click when this button is rendered.

This is the method I used the most for now (probably the dumbest method):

$(function(){
   setTimeout( simulate the click, or w/e I want,  100);
});

I would set the delay by trial & error. While it works most of the time, if I keep refreshing the page, there will be one or two times it fails, as the button is not yet rendered on the document after 100ms. Also I suspect this method is machine / browser dependent...in short it is bad.

I believe there is some much better way to achieve the same effect, what is the common / best practice out there to handle such scneario?

5
  • Short and Simple: window.onload = callback; Commented Jun 2, 2016 at 4:33
  • is it possible to choose a set of DOM, and set them once and for all, say like $(element).load() in JQuery (but it is depreciated) Commented Jun 2, 2016 at 4:37
  • If window is loaded, DOM is loaded for sure.. You ca refer DOMContentLoaded event as well.. Commented Jun 2, 2016 at 4:38
  • @Rayon yea that's true, but is there any chance that there is a small time gap between DOM element loaded & window loaded? if possible, I want to fire the event once the DOM is loaded, even the window is not yet loaded Commented Jun 2, 2016 at 4:42
  • @shole, Refer DOMContentLoaded Commented Jun 2, 2016 at 4:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.