Many examples of using the jQuery DOM ready function are listed here - but they could not mention or demonstrate that you need to enable jQuery to use it.
You can use JavaScript directly to accomplish what you want - but the technique varies in different browsers, and jQuery always does a great job of normalizing these differences in all directions - I personally am a jQuery person, There will probably always be :)
At the end of your <head> HTML document, include the jQuery library and then your code :)
<script src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $(function(){ console.log("DOM is Ready! Images haven't finished loading yet!"); }); </script>
Using the jQuery .ready() function occurs in several forms. Personally, I like the above method. The two below are apparently the most officially justified.
$(document).ready(function(){ console.log("DOM Ready!"); }); $(function(){ console.log("DOM Ready!"); });
The jQuery documentation on .ready() has this to say:
While JavaScript provides a load event to execute code when rendering the page, this event does not fire until all assets, such as images, are fully received. In most cases, a script can be run as soon as the DOM hierarchy is completely built. The handler passed to .ready () should be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it is important to reference external style sheets or embed style elements before referencing scripts.
I highlighted a quote from the docs for you since you mentioned that you want to load CSS first. In appearance, you just need to make sure that the <link rel="stylesheet" href="style.css" /> are located in the <head> section above your jQuery code :)
Happy coding! // Chase
jQuery.com
Chasemoskal
source share