Skip to content

Instantly share code, notes, and snippets.

@chirag64
Created March 27, 2014 05:45
Show Gist options
  • Save chirag64/9801111 to your computer and use it in GitHub Desktop.
Save chirag64/9801111 to your computer and use it in GitHub Desktop.
Lazy Load JS & CSS files example
//Lazy load example
//Load CSS and JS files only if the pre tag elements are present within the article tag
$('article pre').length && (function() {
var mediaPath = '/assets/';
$('<link>').attr({
type: 'text/css',
rel: 'stylesheet',
href: mediaPath + 'css/syntax.css'
}).appendTo(document.head);
var syntaxScript = document.createElement('script');
syntaxScript.async = 'true';
syntaxScript.src = mediaPath + 'js/syntax.js';
doc.body.appendChild(syntaxScript);
})();
//Referenced from http://davidwalsh.name/lazy-load-elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment