Forked from paulirish/JavaScript preload() function
Created
December 28, 2016 07:08
-
-
Save grgur/ba97dc334d8dd6d40e5a9d0a999eb2f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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' | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* $.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