Skip to content

Commit 2a948fe

Browse files
committed
Added the cacheImagesOutput( url )
Kicks out the encoded string for a URL
1 parent 8a11e49 commit 2a948fe

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#jQuery Cache Images plugin
22

3-
Plugin for jQuery that allows for the easy caching of image files in the browsers localstorage. The local storage approach allows the media to persist across sessions, while the browser manages all of the cross-domain privacy protections.
3+
Plugin for jQuery that allows for the easy caching of image files in the browsers localStorage. The local storage approach allows the media to persist across sessions, while the browser manages all of the cross-domain privacy protections.
44

55
## Using the Plugin
66
The plugin can be used two ways. It can either be applied to a specific element in the DOM, or you can apply it to a container that will have images within it. Both approached will bind the Cache Image plugin to all future changes that occur to those elements or their children.
@@ -18,7 +18,7 @@ Each of these is optional.
1818
`$('img#AnElement').cacheImages();`
1919
Any selector here works, however it will bind to only existing elements. If you need to have the caching work on future dynamically created elements use the second approach.
2020

21-
### Attached to a Parent Container *Coming Soon*
21+
### *Attached to a Parent Container **Coming Soon***
2222
`$('div#AwesomeContainer').cacheImages();`
2323
This will watch for changes to the parent and all child elements for changes that involve images. The plugin will step in, cache the image into local storage, and reveil the image to the user.
2424

@@ -29,10 +29,16 @@ Allows you to easily drop caching into your dom additions. It will look at the c
2929
### Manually caching an image
3030
`cacheImagesFetchURL('http://upload.wikimedia.org/wikipedia/commons/9/92/Muraltmuur.jpg');`
3131

32-
Attempts to cache that image into your clients browser local storage. This can be very helpful if you have an app where you are storying data into webSQL or IndexedDB and want to grab images during an initial sync, but those images might not be needed until later. By caching the images earily you ensure that they would be available along with the other data.
32+
Attempts to cache that image into your clients browser local storage. This can be very helpful if you have an app where you are storying data into webSQL or IndexedDB and want to grab images during an initial sync, but those images might not be needed until later. By caching the images early you ensure that they would be available along with the other data.
3333

3434
These images would later be automatically placed due to element or parent binding, or you could manually place them (see below).
3535

36+
### Manually retrieving an image
37+
`cacheImagesOutput('http://upload.wikimedia.org/wikipedia/commons/9/92/Muraltmuur.jpg');`
38+
39+
If you need to use an image in your inline css, or in another context where you just need the encoded string you should use this function. It will return the encoded string if it has already been cached, or it will return the default (if that is already encoded), or null. This will not fetch the URL since that is an asynchronous event that cannot return your output.
40+
41+
3642
# Credits and Thanks
3743
* Based Heavily off of @doomhz plugin [jQueryImageCache](https://github.com/doomhz/jQuery-Image-Cache)
3844
* Utilizing base64 encoding from @mathias [Encoding XHR image data](http://jsperf.com/encoding-xhr-image-data/33)

jquery.cacheImages.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,27 @@
213213
window.cacheImagesFetchURL = function( url ){
214214
$('body').append( $('<img style="display: none;" />').addClass('cacheImagesRemove').cacheImages({url: url}) );
215215
};
216+
/*
217+
* Manually cache an image into the local storage
218+
*/
219+
window.cacheImagesOutput = function( url ){
220+
var tempKey = window.cacheImagesConfig.storagePrefix + ':' + url;
221+
if( window.localStorage.getItem( tempKey ) != null ){
222+
return window.localStorage.getItem( tempKey ); // Image exists in the cache
223+
}else{
224+
225+
if( /^data:image/.test( window.cacheImagesConfig.defaultImage ) === true ){
226+
return window.cacheImagesConfig.defaultImage; // this is an encoded string
227+
}
228+
229+
tempKey = window.cacheImagesConfig.storagePrefix + ':' + window.cacheImagesConfig.defaultImage;
230+
if( window.localStorage.getItem( tempKey ) != null ){
231+
return window.localStorage.getItem( tempKey ); // Default URL was already cached
232+
}
233+
}
234+
235+
return null; // Neither the image or the cached version existsed
236+
};
216237
/*
217238
* Will remove all of the cached images from their localStorage
218239
*/

jquery.cacheImages.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)