Skip to content

Commit

Permalink
Fix weird callstack bug
Browse files Browse the repository at this point in the history
FYI it was triggered on every browsers by the onload= being called at start AND when we switched from data-src -> src
On IE it was trggering the unfamous stack overflow at line 0 bug
Now also a LOT faster motherfucker
  • Loading branch information
vvo committed Mar 2, 2012
1 parent c520c29 commit d1bddf3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
52 changes: 25 additions & 27 deletions LL.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
}

// cross browser event handling
// cross browser event add
function addEvent( el, type, fn ) {
if (el.attachEvent) {
el.attachEvent && el.attachEvent( 'on' + type, fn );
Expand All @@ -23,7 +23,7 @@
}
}

// cross browser event handling
// cross browser event remove
function removeEvent( el, type, fn ) {
if (el.detachEvent) {
el.detachEvent && el.detachEvent( 'on' + type, fn );
Expand All @@ -32,10 +32,10 @@
}
}

// glocal variables
var
lazyAttr = 'data-src',
winH,
imgs = [],
pageHasLoaded,

// cross browser window height
Expand All @@ -44,38 +44,34 @@
(document.documentElement && document.documentElement.clientHeight) ||
(document.body && document.body.clientHeight) ||
10000;
}, 77),
}, 50),

checkImagesThrottle = throttle(function checkImages() {
var
imgs = document.getElementsByTagName('img'),
last = imgs.length,
current,
allImagesDone = true;

for (current = 0; current < last; current++) {
var img = imgs[current];
// if showIfVisible is false, it means we have some waiting images to be
// shown
if(showIfVisible(imgs[current]) === false) {
if(img !== undefined && showIfVisible(img, current) === false) {
allImagesDone = false;
}
}

if (allImagesDone && pageHasLoaded) {
unsubscribe();
}
}, 77);

function showIfVisible(el) {
if (el.getAttribute(lazyAttr) === null) {
// img already shown
return true;
}
}, 5);

function showIfVisible(img, index) {
// 200 is the vertical offset used for preloading soon to be visible images
if (el.getBoundingClientRect().top < winH + 200) {
el.src = el.getAttribute(lazyAttr);
el.removeAttribute(lazyAttr);
if (img.getBoundingClientRect().top < winH + 200) {
img.src = img.getAttribute(lazyAttr);
img.removeAttribute(lazyAttr);
imgs.splice(index, 1);
// img shown
return true;
} else {
Expand All @@ -89,22 +85,24 @@
removeEvent(window, 'scroll', checkImagesThrottle);
}

// Indicates that the page is optimized
window['fstrz'] = true;
// Import any fzns object (Fasterize Name Space)
window['fzns'] = window['fzns'] || {};
// Export and creates LL.s() to be used by <img onload=/>
window['fzns']['LL'] = {
's': function(el) {
// To avoid onload being called and called and called ...
el.onload = null;

This comment has been minimized.

Copy link
@vvo

vvo May 29, 2012

Owner

This is where mod_pagespeed lazyload fails

showIfVisible(el, imgs.push(el) - 1);
}
};

getWindowHeightThrottle();
addEvent(window, 'resize', getWindowHeightThrottle);
addEvent(window, 'scroll', checkImagesThrottle);
addEvent(window, 'load', function() {
pageHasLoaded = true;
});

// Indicates that the page is optimized
window['fstrz'] = true;

// Import any fzns object
window['fzns'] = window['fzns'] || {};

// Export and creates LL.show() to be used by <img onload=/>
window['fzns']['LL'] = {
's': showIfVisible
};

})(this, document)
4 changes: 2 additions & 2 deletions LL.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "LL",
"description": "Lazy load my friend",
"version": "0.4.0",
"version": "0.5.0",
"repository": {
"type": "git",
"url": "http://github.com/fasterize/LL.git"
Expand Down

0 comments on commit d1bddf3

Please sign in to comment.