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:
- Inject
<script src="...">
and<link rel="stylesheet" href="...">
on document.ready - Inject
<script src="...">
and<link rel="stylesheet" href="...">
on window.load - Inject
<script src="...">
and<link rel="stylesheet" href="...">
few seconds after document.ready is fired - Inject
<script src="...">
and<link rel="stylesheet" href="...">
few seconds after window.load is fired
My 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:
- When exactly will it fire the document.ready event
- In what order will it download the files
- Will it block the page while attempting to download the injected files
This may be helpfull: Loading Scripts Without Blocking, but it answer only few questions.