0

I execute this code in the head of the document:

 console.log('DOMContentLoaded event');
 document.addEventListener( "DOMContentLoaded", ready, false );

However, the function doesnt get called, even after the page is ready? What is inside the console log does get printed

Notice that i add a javascript file dynamically (it is not hardcoded), and i think it is quite late in the script.. it is after the window.onload event? may it be the reason? any way I can trigger the event?

BlackFire27
  • 1,470
  • 5
  • 21
  • 32
  • 3
    If you add event listener after the event already happened then yeah, that's a good reason why it won't "work". – Shadow The GPT Wizard Feb 17 '13 at 13:20
  • Sounds like what you are really after is manually calling this event? If so, see [this other question](http://stackoverflow.com/q/9153314/447356). [fiddle](http://jsfiddle.net/jYLcX/). – Shadow The GPT Wizard Feb 17 '13 at 13:23
  • @ShadowWizard: No, that's not really a solution. `ready` functions which do not maintain an internal "called" flag would be distracted by that. – Bergi Feb 17 '13 at 17:04
  • @Bergi true, but looks like that's his best option, no? – Shadow The GPT Wizard Feb 17 '13 at 20:51
  • @ShadowWizard: I'd propose a helper function that knows when the DOM is already loaded and acts appropriate, just like `jQuery.ready` does – Bergi Feb 18 '13 at 08:15

0 Answers0