Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crossai-2033/729152 to your computer and use it in GitHub Desktop.
Save crossai-2033/729152 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,
d = document,
b = d.body,
isIE = /*@cc_on!@*/0;
while (i--) {
if (isIE) {
new Image().src = arr[i];
continue;
};
o = d.createElement('object');
o.data = arr[i];
o.width = o.height = 0;
b.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,
isIE = $.browser.msie,
d = document,
b = d.body,
o;
while (i--) {
if (isIE) {
new Image().src = arr[i];
continue;
};
o = d.createElement('object');
o.data = arr[i];
o.width = o.height = 0;
b.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