├── .project ├── README └── imageCache.js /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | imageCache-git 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This JavaScript library is intended to make image caching as simple as possible. The syntax is: 2 | 3 | var s=["images/1.jpg", "images/2.jpg", "images/3.jpg"]; // or any array of image URLs 4 | imageCache.pushArray(s, loadImageEvent, loadAllEvent); 5 | 6 | loadImageEvent is a callback that fires everytime an image is loaded. 7 | loadAllEvents is a callback that fires when all images are loaded. 8 | 9 | More info can be found at http://www.useragentman.com/blog/?p=4329 10 | -------------------------------------------------------------------------------- /imageCache.js: -------------------------------------------------------------------------------- 1 | /** 2 | * imageCache.js - image caching framework. 3 | * Zoltan Hawryluk - http://www.useragentman.com/ 4 | * MIT License. 5 | */ 6 | 7 | var imageCache = new function () { 8 | var me = this; 9 | 10 | var cache = []; 11 | var root = document.location.href.split('/'); 12 | 13 | root.pop(); 14 | root = root.join('/') + '/'; 15 | 16 | me.push = function (src, loadEvent) { 17 | 18 | if (!src.match(/^http/)) { 19 | src = root + src; 20 | } 21 | 22 | var item = new Image(); 23 | 24 | 25 | if (cache[src] && loadEvent) { 26 | loadEvent(src); 27 | } else { 28 | if (loadEvent) { 29 | item.onload = loadEvent; 30 | item.onerror = loadEvent; 31 | } 32 | cache[src]=item; 33 | } 34 | 35 | item.src = src; 36 | } 37 | 38 | me.pushArray = function (array, imageLoadEvent, imagesLoadEvent) { 39 | var numLoaded = 0; 40 | var arrayLength = array.length; 41 | for (var i=0; i