Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 2
    @Peter pretty sure this stands alone probably best if it in the bottom of your javascript files so it executes last
    – arodebaugh
    Commented Jul 18, 2017 at 14:59
  • 4
    This is the most completed answer. onload is after, but people don't realize this at first.
    – tfont
    Commented Jan 24, 2019 at 10:26
  • 4
    Re asynchronous javascript: to clarify, there are two choices for scripts that don't immediately run. Of those, defer is usually preferable to async - because defer won't interrupt html parsing/rendering - and when it runs, DOM is guaranteed to be ready. Efficiently load JavaScript with defer and async. Commented Apr 30, 2019 at 20:18
  • 2
    +1 I tried window.onload = ... thinking that my script would wait until everything was fully downloaded before running but it seems window.onload actually behaves like document.addEventListener("DOMContentLoaded", ..., whereas the window.addEventListener("load", ... really does wait for everything to be fully downloaded. I would have thought that window.onload should be equivalent to window.addEventListener("load", ... rather than document.addEventListener("DOMContentLoaded", ... ?? I got the same result in Chrome and FF.
    – wkille
    Commented Jan 24, 2020 at 11:58
  • @DevWL, I think you meant "defer"... because "async" can still execute before the all the html loads. Commented Mar 4, 2022 at 17:41