How to run jQuery function after all and any other javascript

I have a photo gallery page hosted on CMS (Squarespace) that has some of its own scripts that load thumbnails asynchronously.

The actual large images, however, are not predefined, so I decided to add it to my own script to make the browser load these large images into the cache in the background, for example:

(function($) { var cache = []; // Arguments are image paths relative to the current page. $.preLoadImages = function() { var args_len = arguments.length; for (var i = args_len; i--;) { var cacheImage = document.createElement('img'); cacheImage.src = arguments[i]; cache.push(cacheImage); } } })(jQuery) $(window).load(function(){ $.preLoadImages( "/picture/1.jpg", "/picture/2.jpg", //etc. ); }); 

I put my code in $ (window) .load (), because it is a background script, and it doesn’t matter that it even runs at all, it’s just to improve performance.

However, I think this script somehow blocks the preloading of its own CMS script thumbnails.

I'm right? And most importantly, is there a way to dictate that my script runs only after all other scripts on the page have been launched?

amuses

+7
javascript jquery asynchronous blocking preloading
source share
2 answers

JavaScript always works, for example, the hover event is constantly being fired, mousemove , etc. .... there is no "end" for running the script.

However, in your case, this should not block any other preloading ... you can also use document.ready here, since you really don't need the images downloaded before your code runs.

In fact, you are actually slowing the page down using window.load instead ... since preloading starts later when it can be parallelized with other downloads by the browser before. Use document.ready instead, for example:

 $(function(){ $.preLoadImages( "/picture/1.jpg", "/picture/2.jpg", //etc. ); }); 
+2
source share

Scripts are loaded from top to bottom, and bodyloadload is usually added to existing onloads - as long as this $ (function () .. is at the end of the page, it will be launched last. (Last (as for nickname comment), meaning initial parsing / start document)

+1
source share

All Articles