I have many images on site and some scripts on it. But scripts can run only after loading whole page. How to accelerate this?
$(document).ready(function() {
//code here
});
will run a script when the document structure is ready, but before all of the images have loaded.
if you want to run script before the document structure is ready, just put your code anywhere.
Sometimes if you only use $(document).ready()
, there will be a flash of content.
To avoid the flash, you can hide the body with css then show it after the page is loaded.
html { visibility:hidden; }
$(document).ready(function() {
//your own JS code here
document.getElementsByTagName("html")[0].style.visibility = "visible";
});
Then the page will go from blank to showing all content when the page is loaded, no flash of content, no watching images load etc.
Inspired by this, thanks to the author.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With