Skip to content

Instantly share code, notes, and snippets.

@revelt
Forked from seantunwin/lazy-load-files.js
Last active April 1, 2016 21:15
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 revelt/f934ece7e1c03a8bb566f8c01c78dda1 to your computer and use it in GitHub Desktop.
Save revelt/f934ece7e1c03a8bb566f8c01c78dda1 to your computer and use it in GitHub Desktop.
Lazy load JavaScript files
/* This is a technique to lazy load your javascript files
* Handy for those pesky slow, load blocking off-site scripts
* function lazyLoad
* @s: String of path to file
*/
function lazyLoad(s) {
var d = window.document;
var b = d.body; /* appends at end of body, but you could use other methods to put where you want */
var e = d.createElement('script');
e.async = true;
e.src = s;
b.appendChild(e);
}
/* Then when document is loaded and ready (this example assumes jQuery)
$(function() {
lazyload("path/to/script");
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment