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

Commit

Permalink
removed the data-attr based approach. Now using ?full=largeimagesrcpa…
Browse files Browse the repository at this point in the history
…th. No ".r" flag is needed anymore either, no gif is used. Thanks Andy Hume for the idea!
  • Loading branch information
scottjehl committed May 24, 2011
1 parent 3a48ca5 commit fdd89a2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 53 deletions.
5 changes: 2 additions & 3 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
# //Start Responsive Images
RewriteEngine On
# direct image requests to temp
RewriteRule .*rwd-router/.*\.r\.(jpe?g|png|gif)$ rwd-images/rwd.gif [L]
RewriteCond %{QUERY_STRING} full=(.*)&?
RewriteRule (.*)rwd-router/.*\.(jpe?g|png|gif) $1%1 [L]
# ignore trap for non-image requests, rewrite URL without trap segment
RewriteRule (.*)rwd-router/(.*)$ $1$2
# remove .r from all requests
RewriteRule (.*)\.r(\.(jpe?g|png|gif))$ $1$2
# //End Responsive Images
2 changes: 1 addition & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h1>Responsive Images Test Page</h1>
<p>In supported browsers, the following image will load either small or large version depending on screen resolution, making a single 1kb request before requesting the appropriate size.</p>

<!-- ADD THE "r." prefix to your image extension for any responsive images -->
<img src="sample-content/running-sml.r.jpg" data-fullsrc="sample-content/running-lrg.jpg">
<img src="sample-content/running-sml.jpg?full=sample-content/running-lrg.jpg" />

<p id="cred"><strong>Photo credit (CC):</strong> Cia de Foto <a href="http://www.flickr.com/photos/ciadefoto/3192757134/in/pool-809956@N25/">Flickr Creative Commons</a></p>

Expand Down
53 changes: 4 additions & 49 deletions rwd-images/rwd-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,8 @@
//add HTML class
doc.documentElement.className += " " + htmlClass;

//find and replace img elements
var findrepsrc = function(){
var imgs = doc.getElementsByTagName( "img" ),
il = imgs.length;

for(var i = 0; i < il; i++){
var img = imgs[i],
fullsrc = img.getAttribute("data-fullsrc");

if(fullsrc){
img.src = fullsrc;
}
}
},
//base tag support test (returns base tag for use if support test passes)
//originally used in the jQuery Mobile framework, converted to plain JS in the hasjs framework, modified for use here
base = (function(){
//base tag support test (returns base tag for use if support test passes)
var base = (function(){
var backup,
baseAdded = false,
a = doc.createElement("a"),
Expand Down Expand Up @@ -74,37 +59,7 @@
base.href = dirPath + "rwd-router/";
}
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;
//making this async seems to ensure images don't double request?
setTimeout(function(){
if( base ) {
//set base back to something real before removing
base.href = dirPath;
head.removeChild(base);
}
findrepsrc();
},0);
};

//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);
Binary file removed rwd-images/rwd.gif
Binary file not shown.

3 comments on commit fdd89a2

@notasausage
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why you decided to switch from the data-attr approach to a query string method for including the larger image file.

@scottjehl
Copy link
Member

@scottjehl scottjehl commented on fdd89a2 May 9, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@notasausage
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Scott. I had already looked at Picturefill, which seemed like another decent approach. Also, there's a new jQuery plugin that is similar called jQuery Picture if you haven't seen that yet:

http://jquerypicture.com

I asked about the data-attr approach because I felt it was more semantic and easier to read, plus it would be easy to extend for more image size options with multiple attributes. Jeremy Keith took this concept and talked about it a bit on his blog, comparing the loading of the mobile image and replacing it with the full image to our old friend the lowsrc attribute:

http://adactio.com/journal/5208/

At any rate, there's no perfect solution at this point, but I really appreciate all your hard work on this stuff so far. Keep it up.

Please sign in to comment.