Franck Freiburger
Franck Freiburger

Reputation: 28448

DOMContentLoaded vs local script

What is the difference between a script called by DOMContentLoaded event handler and a script called at the end of the body ?

Upvotes: 2

Views: 271

Answers (1)

Nickolay
Nickolay

Reputation: 32063

  • Obviously, they execute in slightly different context - the inline script is "top-level" code, the DOMContentLoaded handler is in an event listener function.
  • Perhaps also obviously, DOMContentLoaded is executed some time after the last inline script. See HTML5 specification for the list of steps supposed to happen in between.
  • DOMContentLoaded is useful in cases when you don't control the markup, e.g. library code or something like browser extensions (See also Unobtrusive JavaScript)

Upvotes: 3

Related Questions