Rectangle 27 0

javascript How can I detect DOM ready and add a class without jQuery?


<body>
    <script type="text/javascript">
        document.body.className+= ' javascript';
    </script>

(although in general if that's the aim it's better to remove the fallback elements as you go along replacing them with scripted elements, so that if one piece of script errors out all the other components on the page don't break.)

If your aim is to add the class to body immediately as the page is loaded, perhaps to hide no-JS-fallback elements, you could do that just immediately inside the body tag rather than waiting for any events:

This is the fastest way to bind to elements: do so just immediately after creating them (inside the open tag if you only need to alter the elements; just after the close tag if you need to alter their contents). However this approach does tend to litter the page with ugly <script> blocks, which is why more people put the code all at the bottom or use an load/ready-handler.

Note
Rectangle 27 0

javascript How can I detect DOM ready and add a class without jQuery?


function domReady () {
  document.body.className += " javascript";
  // ...
}

// Mozilla, Opera, Webkit 
if ( document.addEventListener ) {
  document.addEventListener( "DOMContentLoaded", function(){
    document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
    domReady();
  }, false );

// If IE event model is used
} else if ( document.attachEvent ) {
  // ensure firing before onload
  document.attachEvent("onreadystatechange", function(){
    if ( document.readyState === "complete" ) {
      document.detachEvent( "onreadystatechange", arguments.callee );
      domReady();
    }
  });
}

@Jan I'm guessing this was transcribed from the jQuery source which also adds a fallback listener to window.onload. So in the jQuery source, the first time the callback is invoked listeners need to be removed to avoid invoking twice, once for each event. But since this code doesn't include that fallback I don't think it's necessary to remove the listener. But I'm just speculating.

@JanDvorak - In general it is good practice and can be beneficial to performance to remove listeners that are no longer needed.

If you are attempting to replicate the behaviour of jQuery 2.x, it does not include capability detection for document.attachEvent and always uses the DOMContentLoaded event. github.com/jquery/jquery/blob/master/src/core/ready.js

If you want to reproduce the jQuery's document.ready event, you can use the onreadystatechange or DOMContentLoaded events where applicable:

Why are you detaching the readystatechange event? Does IE8 fire that event twice? Why?

Note
Rectangle 27 0

javascript How can I detect DOM ready and add a class without jQuery?


function domReady () {
  document.body.className += " javascript";
  // ...
}

// Mozilla, Opera, Webkit 
if ( document.addEventListener ) {
  document.addEventListener( "DOMContentLoaded", function(){
    document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
    domReady();
  }, false );

// If IE event model is used
} else if ( document.attachEvent ) {
  // ensure firing before onload
  document.attachEvent("onreadystatechange", function(){
    if ( document.readyState === "complete" ) {
      document.detachEvent( "onreadystatechange", arguments.callee );
      domReady();
    }
  });
}

@Jan I'm guessing this was transcribed from the jQuery source which also adds a fallback listener to window.onload. So in the jQuery source, the first time the callback is invoked listeners need to be removed to avoid invoking twice, once for each event. But since this code doesn't include that fallback I don't think it's necessary to remove the listener. But I'm just speculating.

@JanDvorak - In general it is good practice and can be beneficial to performance to remove listeners that are no longer needed.

If you are attempting to replicate the behaviour of jQuery 2.x, it does not include capability detection for document.attachEvent and always uses the DOMContentLoaded event. github.com/jquery/jquery/blob/master/src/core/ready.js

If you want to reproduce the jQuery's document.ready event, you can use the onreadystatechange or DOMContentLoaded events where applicable:

Why are you detaching the readystatechange event? Does IE8 fire that event twice? Why?

Note
Rectangle 27 0

javascript How can I detect DOM ready and add a class without jQuery?


<body>
    <script type="text/javascript">
        document.body.className+= ' javascript';
    </script>

(although in general if that's the aim it's better to remove the fallback elements as you go along replacing them with scripted elements, so that if one piece of script errors out all the other components on the page don't break.)

If your aim is to add the class to body immediately as the page is loaded, perhaps to hide no-JS-fallback elements, you could do that just immediately inside the body tag rather than waiting for any events:

This is the fastest way to bind to elements: do so just immediately after creating them (inside the open tag if you only need to alter the elements; just after the close tag if you need to alter their contents). However this approach does tend to litter the page with ugly <script> blocks, which is why more people put the code all at the bottom or use an load/ready-handler.

Note
Rectangle 27 0

javascript How can I detect DOM ready and add a class without jQuery?


function domReady () {
  document.body.className += " javascript";
  // ...
}

// Mozilla, Opera, Webkit 
if ( document.addEventListener ) {
  document.addEventListener( "DOMContentLoaded", function(){
    document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
    domReady();
  }, false );

// If IE event model is used
} else if ( document.attachEvent ) {
  // ensure firing before onload
  document.attachEvent("onreadystatechange", function(){
    if ( document.readyState === "complete" ) {
      document.detachEvent( "onreadystatechange", arguments.callee );
      domReady();
    }
  });
}

@Jan I'm guessing this was transcribed from the jQuery source which also adds a fallback listener to window.onload. So in the jQuery source, the first time the callback is invoked listeners need to be removed to avoid invoking twice, once for each event. But since this code doesn't include that fallback I don't think it's necessary to remove the listener. But I'm just speculating.

@JanDvorak - In general it is good practice and can be beneficial to performance to remove listeners that are no longer needed.

If you are attempting to replicate the behaviour of jQuery 2.x, it does not include capability detection for document.attachEvent and always uses the DOMContentLoaded event. github.com/jquery/jquery/blob/master/src/core/ready.js

If you want to reproduce the jQuery's document.ready event, you can use the onreadystatechange or DOMContentLoaded events where applicable:

Why are you detaching the readystatechange event? Does IE8 fire that event twice? Why?

Note
Rectangle 27 0

javascript How can I detect DOM ready and add a class without jQuery?


<body>
    <script type="text/javascript">
        document.body.className+= ' javascript';
    </script>

(although in general if that's the aim it's better to remove the fallback elements as you go along replacing them with scripted elements, so that if one piece of script errors out all the other components on the page don't break.)

If your aim is to add the class to body immediately as the page is loaded, perhaps to hide no-JS-fallback elements, you could do that just immediately inside the body tag rather than waiting for any events:

This is the fastest way to bind to elements: do so just immediately after creating them (inside the open tag if you only need to alter the elements; just after the close tag if you need to alter their contents). However this approach does tend to litter the page with ugly <script> blocks, which is why more people put the code all at the bottom or use an load/ready-handler.

Note