Skip to content

Instantly share code, notes, and snippets.

@seantunwin
Last active June 10, 2021 06:07
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save seantunwin/6297759 to your computer and use it in GitHub Desktop.
Save seantunwin/6297759 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,
b = d.body, /* appends at end of body, but you could use other methods to put where you want */
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");
});
@revelt
Copy link

revelt commented Apr 1, 2016

ahem, is it me or two var's are missing on lines 9 and 10?

@LeoAref
Copy link

LeoAref commented Jan 3, 2017

@revelt, yeah, you're right

@seantunwin
Copy link
Author

Fixed. I accidentally used semi-colons instead of commas on those 2 lines.
Thanks for the catch. :-)

@helmiItsavirus
Copy link

@seanynwin

did you make a typo in the method name?
lazyload vs lazyLoad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment