Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
set responsive image sources on DOM ready, with window load as a fall…
Browse files Browse the repository at this point in the history
…back
  • Loading branch information
scottjehl committed Jan 19, 2011
1 parent 6a05d7c commit 29b1e7e
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions rwd-images/rwd-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
doc = win.document,
head = doc.getElementsByTagName('head')[0],

//record existing window onload handler
winload = win.onload,

//record width cookie for subsequent loads
recordRes = (function(){
var date = new Date();
Expand Down Expand Up @@ -93,18 +90,34 @@
}
}
return base;
})();
})(),

//flag for whether loop has run already
complete = false,

//remove base if present, find/rep image srcs if wide enough (maybe make this happen at domready?)
readyCallback = function(){
if( complete ){ return; }
complete = true;
if( base ) {
//set base back to something real before removing
base.href = dirPath;
head.removeChild(base);
}
findrepsrc();
};

//on load, remove base if present, find/rep image srcs if wide enough (maybe make this happen at domready?)
win.onload = function(){
if( base ) {
//set base back to something real before removing
base.href = dirPath;
head.removeChild(base);
}
findrepsrc();
if( winload ){
winload();
}
};
//DOM-ready or onload handler
//W3C event model
if ( doc.addEventListener ) {
doc.addEventListener( "DOMContentLoaded", readyCallback, false );
//fallback
win.addEventListener( "load", readyCallback, false );
}
// If IE event model is used
else if ( doc.attachEvent ) {
doc.attachEvent("onreadystatechange", readyCallback );
//fallback
win.attachEvent( "onload", readyCallback );
}
})(this);

0 comments on commit 29b1e7e

Please sign in to comment.