I know the difference between document.ready and window.load events. Now in order to improve the performance of my website, I plan to defer loading of javascript. I've seen numerous examples of lazy-loading content:
<script src="...">
and <link rel="stylesheet" href="...">
on document.ready<script src="...">
and <link rel="stylesheet" href="...">
on window.load<script src="...">
and <link rel="stylesheet" href="...">
few seconds after document.ready is fired<script src="...">
and <link rel="stylesheet" href="...">
few seconds after window.load is firedMy first question is which of these methods is preferable.
My second question is that that I want to know what exactly happens when I use approach #1. If my document has this content defined in HTML source:
<!-- head -->
<link>
<script>
<!-- body -->
and in document.ready I inject these additional tags:
<!-- head -->
<link>
<script>
<link class="lazyload">
<script class="lazyload">
<!-- body -->
<img><img><img><img><img>
I am wondering what exactly will the browser do:
This may be helpfull: Loading Scripts Without Blocking, but it answer only few questions.