Skip to content

Instantly share code, notes, and snippets.

@sebarmeli
Created October 16, 2012 13:40
Show Gist options
  • Save sebarmeli/3899364 to your computer and use it in GitHub Desktop.
Save sebarmeli/3899364 to your computer and use it in GitHub Desktop.
Lazy-loading Script using yepnope.js
function loadWhenVisible(domElement, script, callback) {
var rect = domElement.getBoundingClientRect();
if (rect.top >= 0 && rect.left >= 0 &&
rect.bottom <= window.innerHeight &&
rect.right <= window.innerWidth) {
yepnope.injectJs(script, function(){
if (callback) { callback(); }
});
}
$(function(){
var loadScript = function(){
var anchor = document.getElementById("my-id");
loadWhenVisible(anchor, "js/third_party.js", function(){
console.log("Hi!");
});
};
$(window).on('resize', loadScript).on('scroll', loadScript);
});}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment