I did search on Google to understand how browser loads the resources (CSS, JS, Images, HTML etc) and when I did the prototype code then got little confused based on my test. Following is my plnkr code where I added <script> to add delay of 10 seconds to test the document load vs window load.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script data-require="jquery" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// executes when HTML-Document is loaded and DOM is ready
console.log("document loaded at "+new Date());
});
$(window).load(function() {
// executes when complete page is fully loaded, including all frames, objects and images
console.log("window loaded at "+new Date());
});
</script>
</head>
<body>
<h1 class="red">First element :-)</h1>
<script src="https://httpbin.org/delay/60"></script>
<h1 class="blue">Second element:-)</h1>
</body>
</html>
I have following observations and questions
My understanding was browser doesn't render the page until complete page is not parsed i.e. DOM is prepared. However once I tested this sample page then I do see my first H1 tag is rendered and then it waited to load the next
<script>and then it renders the second H1 tag. Isn't page should wait to build the complete DOM first then try to render?Why $(window).load and $(document).ready events are getting fired at same moment?
load. Also note that those two events details are very browser-specific. jQuery tries to smooth things out, but I'm not sure you can count on a 100% exact same behaviour on all browsers.cssfiles that they are currently award if you have linked acssfile in yourheadand you have a delay on the delivery on this one, then you most likely will see a blank screen or the previous one until the style-sheet is loaded.