So if a certain image or other resource takes a long time to load, the visitor will see an incomplete page, or even a script that relies on dynamically added elements will be executed before the image is loaded, resulting in a script error.
window .onload = function() { testDiv.innerHTML = "
Dynamicly created div
"; }
The solution is to wait for the DOM to be parsed, Execute our function before images and external resources are loaded. To make this implementation feasible in jQuery:
//jQuery uses the dynamically created $(document).ready(function) method
$(document).ready(
function() { testDiv.innerHTML = "
Use the dynamically created $(document).ready(function) method
"; }
);
//Or easy to use Syntax:
/jQuery Use $(function) method
$(
function() { testDiv.innerHTML = "
Use $(function) Method
"; }
);