jQuery users are happy, they have:
1 |
$( document ).ready |
If you wish to have a vanilla JavaScript solution, this is the one I’m using:
1 2 3 4 5 6 7 8 9 10 11 12 |
<script> function ready(callback){ // in case the document is already rendered if (document.readyState!='loading') callback(); // modern browsers else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback); // IE <= 8 else document.attachEvent('onreadystatechange', function(){ if (document.readyState=='complete') callback(); }); } </script> |
Usage:
1 2 3 4 5 |
<script> ready(function(){ your code here! }); </script> |