Skip to content

Commit d92a8de

Browse files
committed
Implement the Default image
Also added a few more notes
1 parent 16ebbb2 commit d92a8de

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

demo.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ <h1>jQuery Cache Images v1.0 Plug-in</h1>
2323
</p>
2424
<div id="statWindow" style="text-align: center; border: 5px solid; min-height: 40px;"></div>
2525

26-
<p>Images are kept in browser's Local Storage as a base64 encoded string. </p>
26+
<p>Images are kept in browser's Local Storage as a base64 encoded string. Below are three images and an image that links to a non-existant file so it shows the default.</p>
2727

2828
<div id="DemoImages">
2929
<img src="assets/x-all-the-things.png" />
3030
<img src="assets/cat.gif" />
3131
<img src="assets/warhol.jpg" />
32+
<img src="assets/broken-image-path.jpg" />
3233
</div>
3334

3435

@@ -46,6 +47,7 @@ <h2>Flickr Test</h2>
4647

4748
<script type="text/javascript">
4849
$('#DemoImages img').cacheImages();
50+
// $('#DemoImages img').cacheImages({defaultImage: 'http://upload.wikimedia.org/wikipedia/commons/f/f8/Monocle_with_gallery.jpg'});
4951
$(function(){
5052
// $('#flickrTest').cacheImages({debug: true});
5153
$(document).on('click','#dropCache', function(){ cacheImagesDrop(); });

jquery.cacheImages.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232

3333
var self = this;
3434

35+
36+
/*
37+
* Ensure we have the default image cached and ready for use
38+
*/
39+
if( /^data:image/.test( window.cacheImagesConfig.defaultImage ) === false ){
40+
window.cacheImagesConfig.defaultSrc = window.cacheImagesConfig.defaultImage;
41+
}
42+
43+
3544
/*
3645
* Takes the image and uses a canvas element to encode the media
3746
* response | string | this is the raw XHR resposne data
@@ -141,7 +150,7 @@
141150
$this.prop('src', localSrcEncoded);
142151
}else{
143152
// The image has not yet been cached, so we need to get on that.
144-
$this.prop('src', src);
153+
$this.prop('src', ''); // This will cancel the request if it hasn't already been finished
145154
var imgType = src.match(/\.(jpg|jpeg|png|gif)$/i); // Break out the filename to get the type
146155
if( imgType && imgType.length){ // Get us the type of file
147156
imgType = imgType[1].toLowerCase() == 'jpg' ? 'jpeg' : imgType[1].toLowerCase();
@@ -158,7 +167,7 @@
158167
localStorage[key] = src = base64EncodeCanvas( img );
159168
if( src.substring(0,5) === 'data:' ){
160169
$this.prop('src', localStorage[key] );
161-
if( $this.data('cacheImagesRemove') === 'true' ){
170+
if( $this.is('.cacheImagesRemove') ){
162171
$this.remove();
163172
}
164173
}
@@ -167,15 +176,26 @@
167176
var xhr = new XMLHttpRequest();
168177
xhr.open('GET', src, true);
169178
xhr.responseType = 'arraybuffer'; // Cannot use the jQuery ajax method until it support this response type
170-
xhr.onload = function( e ) {
171-
if (this.status == 200){
179+
xhr.onload = function( e ){
180+
if (this.status == 200 && e.totalSize > 0 ){
172181
localStorage[key] = 'data:image/' + imgType + ';base64,' + base64EncodeResponse( this.response );
173182
$this.prop('src', localStorage[key] );
174-
if( $this.data('cacheImagesRemove') === 'true' ){
183+
if( $this.is('.cacheImagesRemove') ){
175184
$this.remove();
176185
}
177186
}else{
178187
localStorage[key] = 'error';
188+
// Display the default image
189+
if( typeof window.cacheImagesConfig.defaultSrc !== 'undefined' ){
190+
var defaultKey = window.cacheImagesConfig.storagePrefix + ':' + window.cacheImagesConfig.defaultSrc;
191+
if( typeof localStorage[defaultKey] !== 'undefined' ){
192+
$this.prop('src', localStorage[defaultKey] );
193+
}else{
194+
$this.cacheImages({url: window.cacheImagesConfig.defaultSrc }); // Will cache it, and display it here
195+
}
196+
}else{
197+
$this.prop('src', window.cacheImagesConfig.defaultImage );
198+
}
179199
}
180200
};
181201
xhr.send();
@@ -191,7 +211,7 @@
191211
* Manually cache an image into the local storage
192212
*/
193213
window.cacheImageFetchURL = function( url ){
194-
$('body').append($('<img style="display: none;" />').prop('src', url ).data('cacheImagesRemove', 'true').cacheImages());
214+
$('body').append( $('<img style="display: none;" />').addClass('cacheImagesRemove').cacheImages({url: url}) );
195215
};
196216
/*
197217
* Will remove all of the cached images from their localStorage

0 commit comments

Comments
 (0)