Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Improving the Application Code The DOMContentLoaded Event

DOMContentLoaded is not firing

I only get the desired outcome when I use:

window.addEventListener('load', () => {
    // code here
});

I am running ubuntu and using the latest firefox browser.

Anyone know why this is?

2 Answers

Tom Geraghty
Tom Geraghty
24,174 Points

97.67% of browsers support DOMContentLoaded according to CanIUse. Paste in your code and we'll see. Should be something like this:

<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
  });
</script>
Justin Hicks
Justin Hicks
14,290 Points

Bubbling for this is supported by at least firefox Gecko 1.9.2 so if you have the latest then that shouldn't be an issue. Best advice I can supply you with is just look closely for a typo.