Skip to content

Instantly share code, notes, and snippets.

@cobbman
Created August 5, 2016 20:05
Show Gist options
  • Save cobbman/f08cc0c9e86797eb6a5f4820b767eb8e to your computer and use it in GitHub Desktop.
Save cobbman/f08cc0c9e86797eb6a5f4820b767eb8e to your computer and use it in GitHub Desktop.
Preload js, css, and images with AJAX
# Please reference this article for the full description: https://perishablepress.com/3-ways-preload-images-css-javascript-ajax/
window.onload = function() {
setTimeout(function() {
// XHR to request a JS and a CSS
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.js');
xhr.send('');
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.css');
xhr.send('');
// preload image
new Image().src = "http://domain.tld/preload.png";
}, 1000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment