Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grgur/ba97dc334d8dd6d40e5a9d0a999eb2f8 to your computer and use it in GitHub Desktop.
Save grgur/ba97dc334d8dd6d40e5a9d0a999eb2f8 to your computer and use it in GitHub Desktop.
/*!
* JavaScript preload() function
* Preload images, CSS and JavaScript files without executing them
* Script by Stoyan Stefanov – http://www.phpied.com/preload-cssjavascript-without-execution/
* Slightly rewritten by Mathias Bynens – http://mathiasbynens.be/
* Demo: http://mathiasbynens.be/demo/javascript-preload
*/
function preload(arr) {
var i = arr.length,
o,
isIE = /*@cc_on!@*/0;
while (i--) {
if (isIE) {
new Image().src = arr[i];
continue;
};
o = document.createElement('object');
o.data = arr[i];
o.width = o.height = 0;
document.body.appendChild(o);
};
};
// Example:
preload([
'http://tools.w3clubs.com/pagr2/1.sleep.expires.png',
'http://tools.w3clubs.com/pagr2/1.sleep.expires.js',
'http://tools.w3clubs.com/pagr2/1.sleep.expires.css'
]);
/*!
* $.preload() function for jQuery
* Preload images, CSS and JavaScript files without executing them
* Script by Stoyan Stefanov – http://www.phpied.com/preload-cssjavascript-without-execution/
* Slightly rewritten by Mathias Bynens – http://mathiasbynens.be/
* Demo: http://mathiasbynens.be/demo/javascript-preload
* Note that since this script relies on jQuery, the preloading process will not start until jQuery has finished loading.
*/
$.extend({
preload: function(arr) {
var i = arr.length,
o;
while (i--) {
if ($.browser.msie) {
new Image().src = arr[i];
continue;
};
o = document.createElement('object');
o.data = arr[i];
o.width = o.height = 0;
document.body.appendChild(o);
};
}
});
// Example:
$(function() {
$.preload([
'http://tools.w3clubs.com/pagr2/1.sleep.expires.png',
'http://tools.w3clubs.com/pagr2/1.sleep.expires.js',
'http://tools.w3clubs.com/pagr2/1.sleep.expires.css'
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment