0

I am dynamically adding an element (created by document.createElement( "div" )) and I want to know when the element is fully visible, styled and added. I know there's the event DOMNodeInserted, but is this fired when a node is inserted by the browser may not have applied all styling and the rendering/repainting might not be done? If so, is there an event for after a node insertion + styling + repainting is done?

Don Rhummy
  • 20,170
  • 31
  • 134
  • 252
  • 3
    Nope, but as inserting nodes is not asyncronous, all you have to do is place the next line of code after the code that inserts the element. – adeneo May 03 '13 at 21:04
  • Inserting a node will trigger a repaint synchronously. – Fabrício Matté May 03 '13 at 21:05
  • Wouldn't the onload event do the trick for you? – Geeky Guy May 03 '13 at 21:05
  • While javascript code is synchronous and not multi-threaded, there's no guarantee the browser will do the same. They could handle the repainting asynchronously. – Don Rhummy May 03 '13 at 21:06
  • 1
    @DonRhummy No. That'd completely obliterate most of feature detection libraries. Try to find an actual example where this fails if you don't believe it. Of course, the only things that are loaded asynchronously are those that require loading external resources (`img`, `script` and `link rel=stylesheet` elements for example). – Fabrício Matté May 03 '13 at 21:07
  • @FabrícioMatté OK, so then DOMNodeInserted isn't even needed? I can just put something right after the `parentNode.appendChild()` call? – Don Rhummy May 03 '13 at 21:08
  • Yeah exactly, just as Adeneo has said earlier as well. `DOMNodeInserted` is an ugly and deprecated way to observe DOM changes (it has been replaced by [DOM Mutation observers](https://developer.mozilla.org/en-US/docs/DOM/MutationObserver) but it is off-topic to the question). – Fabrício Matté May 03 '13 at 21:09
  • 2
    `DOMNodeInserted` has been deprecated, but it was more intended for cases where you did'nt know when or if an element would be inserted. When you're inserting the element yourself, you know when it's happening, and any code reliant on such an insertion can be added later in the code, as it's synchronous. – adeneo May 03 '13 at 21:17

0 Answers0