Permalink
Browse files

Prevent multiple request for the same image

  • Loading branch information...
Dumitru
Dumitru committed Mar 25, 2011
1 parent 251094c commit 13a28583bde3002fb2c8c8135964ef4735058bd2
Showing with 10 additions and 5 deletions.
  1. +8 −5 assets/jquery.imageCache.js
  2. +2 −0 demo.html
@@ -32,13 +32,16 @@
var src = $(img).attr('src') || $(img).attr('data-src');
if (localStorage) {
var localSrc = localStorage[src];
- if (localSrc) {
+ if (localSrc != null && localSrc != 'false') {
$(img).attr('src', localSrc);
} else {
- $.get(self.config.base64ImageEncoderPath + src, function (data) {
- localStorage[src] = data;
- $(img).attr('src', data);
- });
+ $(img).attr('src', src);
+ if (localStorage[src] === null) {
+ localStorage[src] = 'false';
+ $.get(self.config.base64ImageEncoderPath + src, function (data) {
+ localStorage[src] = data;
+ });
+ }
}
}
});
View
@@ -24,6 +24,8 @@ <h1>jQuery Image Cache plugin</h1>
<ul>
<li><img src="" data-src="assets/test-image-4.jpg" class="imageCache" /></li>
<li><img src="" data-src="assets/test-image-5.jpg" class="imageCache" /></li>
+ <li><img src="" data-src="assets/test-image-5.jpg" class="imageCache" /></li>
+ <li><img src="" data-src="assets/test-image-5.jpg" class="imageCache" /></li>
</ul>
<script type="text/javascript">

0 comments on commit 13a2858

Please sign in to comment.