Skip to content

Instantly share code, notes, and snippets.

@Narayon
Forked from simaovergalho/jQuery-preloadFunction
Last active April 8, 2018 02:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Narayon/bf7246910e10165773ac to your computer and use it in GitHub Desktop.
Save Narayon/bf7246910e10165773ac to your computer and use it in GitHub Desktop.
jQuery: Preload Images, CSS and JS
/*!
* $.preload() function for jQuery – http://mths.be/preload
* 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/
* Note that since this script relies on jQuery, the preloading process will not start until jQuery has finished loading.
*/
jQuery.preload = function(array) {
var length = array.length,
document = window.document,
body = document.body,
isIE = 'fileSize' in document,
object;
while (length--) {
if (isIE) {
new Image().src = array[length];
continue;
}
object = document.createElement('object');
object.data = array[length];
object.width = object.height = 0;
body.appendChild(object);
}
};
// Example:
$(function() {
$.preload([
'http://hang.nodester.com/foo.png?2000',
'http://hang.nodester.com/foo.js?2000',
'http://hang.nodester.com/foo.css?2000'
]);
});
/*!
* $.preload() function for jQuery – http://mths.be/preload
* 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/
* Note that since this script relies on jQuery, the preloading process will not start until jQuery has finished loading.
*/
jQuery.preload = function(array) {
var length = array.length,
document = window.document,
body = document.body,
isIE = 'fileSize' in document,
object;
while (length--) {
if (isIE) {
new Image().src = array[length];
continue;
}
object = document.createElement('object');
object.data = array[length];
object.width = object.height = 0;
body.appendChild(object);
}
};
// Example:
$(function() {
$.preload([
'http://hang.nodester.com/foo.png?2000',
'http://hang.nodester.com/foo.js?2000',
'http://hang.nodester.com/foo.css?2000'
]);
});
/*!
* JavaScript preload() function – http://mths.be/preload
* 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/
*/
function preload(array) {
var length = array.length,
document = window.document,
body = document.body,
isIE = 'fileSize' in document,
object;
while (length--) {
if (isIE) {
new Image().src = array[length];
continue;
}
object = document.createElement('object');
object.data = array[length];
object.width = object.height = 0;
body.appendChild(object);
}
}
// Example:
preload([
'http://hang.nodester.com/foo.png?2000',
'http://hang.nodester.com/foo.js?2000',
'http://hang.nodester.com/foo.css?2000'
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment