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?

shole
  • 4,046
  • 2
  • 29
  • 69
  • Short and Simple: `window.onload = callback;` – Rayon Jun 02 '16 at 04: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) – shole Jun 02 '16 at 04:37
  • If `window` is loaded, DOM is loaded for sure.. You ca refer `DOMContentLoaded` event as well.. – Rayon Jun 02 '16 at 04: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 – shole Jun 02 '16 at 04:42
  • @shole, Refer [__`DOMContentLoaded`__](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded) – Rayon Jun 02 '16 at 04:43

0 Answers0