Stefan Weiss <krewecherl@gmail.com> writes:
>Personally, I wouldn't use any of these. I would even go a step in the
>opposite direction and use the more verbose `addEventListener()` method
>instead of the DOM-0 style "onload" and "onclick" properties:
> window.addEventListener("load", function () {
> // etc
> });
When is it better or worse to use »DOMContentLoaded« instead
of »load«?
When is it better or worse to use »body.« instead of
»window.« above?
I am looking for a »default call« to start my scripts that
should make sure that most of what usually is needed for a
script has been loaded, and so far I had settled for
document.addEventListener( "DOMContentLoaded", ...
>Most people use libraries to simplify common tasks like event handling
>and element selection. With one such library, your code might look like
>this:
I would not use jQuery for »simplification« of a specific
task in a specific browser, but rather because - as far as
I have heard about it - it contains code that already
contains several adaptions to specific browsers (and their
deficiencies and bugs), so, I'd use it because it can act
as a kind of compatibility layer to different browsers.
Otherwise, for its pure features, the distance between
jQuery and plain JavaScript has become a little bit smaller
because some features of jQuery now have been built into
DOM4 by functions such as - for example - »querySelector«.
I wonder whether it has been really this way historically:
was it an invention of jQuery to use CSS selectors to select
elements and is »querySelector« in DOM4 today really because
of the prior art (one might say, »pressure«) of jQuery?
>That's shorter (and also uses the "DOMready" event). Loading a huge
>library for just a few lines of code would be counterproductive, but
>typically the library gets used for the rest of the code, as well.
So, there is a »DOMready«, too! Now I have »load«,
»DOMContentLoaded« /and/ »DOMready« to choose from!