So for some reason it is not working and errors are getting generated now so I google around and I can't figure this out so how can I keep the code pretty much the same with a small enough changes to make it work with ..DOMContentLoaded..? addEventListener ('DOMContentLoaded', function (event) { //the event occurred }) for example if I’m adding the event listener inside a loop and I don’t really know what this will be when the event is triggered. I'm trying to detect when the DOM is ready using DOMContentLoaded. document.addEventListener("DOMContentLoaded", function(e) { console.log("The DOM has finished loading. The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, frame etc. Definition and Usage. addEventListener (" DOMContentLoaded ", (event) => {console. The addEventListener() method attaches an event handler to the specified element. I'm injecting the following JavaScript code into a page: var script = document.createElement('script'); script.type = 'text/ The load event as distinct from DOMContentLoaded only fires once the DOM and all associated resources (like CSS files, JS files, images, external resources, etc.) document. "); }); Load. A simple solution for this is to add a check to the document readyState. Thus it can result in significant performance gains. The DOMContentLoaded fires when the DOM content is loaded, without waiting for … document.onload: 当整个html文档加载的时候就触发了,也就是在body元素加载之前就开始执行了 DOMContentLoaded: 当页面的DOM树解析好并且需要等待JS执行完才触发 DOMContentLoaded事件不直接等待CSS文件、图片的加载完成 This new event is triggered before the load event so you don't have to wait for the entire page to load. Tip: Use the document.addEventListener() method to attach an event handler to the document. A description of the DOMContentLoaded event and why it may be preferable to use over the window.onload event. I have replaced window.addEventListener('DOMContentLoaded', function() {}); with jQuery's $(document).bind('ready', function() {});, because first one failed to work on IE < 9 and I did not wanted to play with .attachEvent() for that dummy browser, if I could have this nicely covered by jQuery itself.. Summary: in this tutorial, you will learn about the JavaScript DOMContentLoaded event. document. addEventListener takes 3 args – stark Mar 16 '13 at 13:12 Thanks for the input, added the false argument in the fiddle without any luck unfortunately :( – Bunker Mar 16 '13 at 13:20 add a comment | Tip: Use the removeEventListener() method to remove an event handler that has been attached with the addEventListener() method. log (' DOM is ready. DOMContentLoaded event gets executed once the basic HTML document is loaded and its parsing has taken place. Syntax: document.addEventListener("DOMContentLoaded", function(e) { console.log("GfG page has … document.addEventListener('DOMContentLoaded', function(){... for making it possible to put it on top. have all finished loading. The issue I found was that DOMContentLoaded won't fire for javascript unless the event is created inline within the document itself. This event doesn’t wait for the completion of the loading of add-ons such as stylesheets, sub-frames and images/pictures. to finish loading.