I’m wanting to add a class to the body tag without waiting for the DOM to load, but I’m wanting to know if the following approach would be valid. I’m more concerned with validity than whether the browsers support it for now.
<body> $('body').addClass('active'); ... </body>
Thanks, Steve
The .elementReady() plugin seems to be pretty close to what you’re looking for.
It operates by using a
setIntervalloop, that exits as soon asdocument.getElementById()returns an element for a givenid.You could probably do a slight modification of that plugin (or commit an update/patch) to allow for generic selectors (at least for ‘tagNames’) instead of just
ids.I don’t believe there is any truly reliable cross-browser compatible way to address an element before it’s loaded – other than this sort of
setIntervalhackingUnless you are able to place your javascript command inside the target element like @JasonBunting suggests.