1/** 2 * jQuery-Plugin "preloadCssImages" 3 * by Scott Jehl, [email protected] 4 * http://www.filamentgroup.com 5 * reference article: http://www.filamentgroup.com/lab/update_automatically_preload_images_from_css_with_jquery/ 6 * demo page: http://www.filamentgroup.com/examples/preloadImages/index_v2.php 7 * 8 * Copyright (c) 2008 Filament Group, Inc 9 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses. 10 * 11 * Version: 5.0, 10.31.2008 12 * Changelog: 13 * 02.20.2008 initial Version 1.0 14 * 06.04.2008 Version 2.0 : removed need for any passed arguments. Images load from any and all directories. 15 * 06.21.2008 Version 3.0 : Added options for loading status. Fixed IE abs image path bug (thanks Sam Pohlenz). 16 * 07.24.2008 Version 4.0 : Added support for @imported CSS (credit: http://marcarea.com/). Fixed support in Opera as well. 17 * 10.31.2008 Version: 5.0 : Many feature and performance enhancements from trixta 18 * -------------------------------------------------------------------- 19 */ 20 21;jQuery.preloadCssImages = function(settings){ 22 settings = jQuery.extend({ 23 statusTextEl: null, 24 statusBarEl: null, 25 errorDelay: 999, // handles 404-Errors in IE 26 simultaneousCacheLoading: 2 27 }, settings); 28 var allImgs = [], 29 loaded = 0, 30 imgUrls = [], 31 thisSheetRules, 32 errorTimer; 33 34 function onImgComplete(){ 35 clearTimeout(errorTimer); 36 if (imgUrls && imgUrls.length && imgUrls[loaded]) { 37 loaded++; 38 if (settings.statusTextEl) { 39 var nowloading = (imgUrls[loaded]) ? 40 'Now Loading: <span>' + imgUrls[loaded].split('/')[imgUrls[loaded].split('/').length - 1] : 41 'Loading complete'; // wrong status-text bug fixed 42 jQuery(settings.statusTextEl).html('<span class="numLoaded">' + loaded + '</span> of <span class="numTotal">
42' + imgUrls.length + '</span> loaded (<span class="percentLoaded">' + (loaded / imgUrls.length * 100).toFixed(0) + '%</span>) <span class="currentImg">' + nowloading + '</span></span>'); 43 } 44 if (settings.statusBarEl) { 45 var barWidth = jQuery(settings.statusBarEl).width(); 46 jQuery(settings.statusBarEl).css('background-position', -(barWidth - (barWidth * loaded / imgUrls.length).toFixed(0)) + 'px 50%'); 47 } 48 loadImgs(); 49 } 50 } 51 52 function loadImgs(){ 53 //only load 1 image at the same time / most browsers can only handle 2 http requests, 1 should remain for user-interaction (Ajax, other images, normal page requests...) 54 // otherwise set simultaneousCacheLoading to a higher number for simultaneous downloads 55 if(imgUrls && imgUrls.length && imgUrls[loaded]){ 56 var img = new Image(); //new img obj 57 img.src = imgUrls[loaded]; //set src either absolute or rel to css dir 58 if(!img.complete){ 59 jQuery(img).bind('error load onreadystatechange', onImgComplete); 60 } else { 61 onImgComplete(); 62 } 63 errorTimer = setTimeout(onImgComplete, settings.errorDelay); // handles 404-Errors in IE 64 } 65 } 66 67 function parseCSS(sheets, urls) { 68 var w3cImport = false, 69 imported = [], 70 importedSrc = [], 71 baseURL; 72 var sheetIndex = sheets.length; 73 while(sheetIndex--){//loop through each stylesheet 74 75 var cssPile = '';//create large string of all css rules in sheet 76 77 if(urls && urls[sheetIndex]){ 78 baseURL = urls[sheetIndex]; 79 } else { 80 var csshref = (sheets[sheetIndex].href) ? sheets[sheetIndex].href : 'window.location.href'; 81 var baseURLarr = csshref.split('/');//split href at / to make array 82 baseURLarr.pop();//remove file path from baseURL array 83 baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir) 84 if (baseURL) { 85 baseURL += '/'; //tack on a / if needed 86 } 87 } 88 if(sheets[sheetIndex].cssRules || sheets[sheetIndex].rules){ 89 thisSheetRules = (sheets[sheetIndex].cssRules) ? //->>> http://www.quirksmode.org/dom/w3c_css.html 90 sheets[sheetIndex].cssRules : //w3 91 sheets[sheetIndex].rules; //ie 92 var ruleIndex = thisSheetRules.length; 93 while(ruleIndex--){ 94 if(thisSheetRules[ruleIndex].style && thisSheetRules[ruleIndex].style.cssText){ 95 var text = thisSheetRules[ruleIndex].style.cssText; 96 if(text.toLowerCase().indexOf('url') != -1){ // only add rules to the string if you can assume, to find an image, speed improvement 97 cssPile += text; // thisSheetRules[ruleIndex].style.cssText instead of thisSheetRules[ruleIndex].cssText is a huge speed improvement 98 } 99 } else if(thisSheetRules[ruleIndex].styleSheet) { 100 imported.push(thisSheetRules[ruleIndex].styleSheet); 101 w3cImport = true; 102 } 103 104 } 105 } 106 //parse cssPile for image urls 107 var tmpImage = cssPile.match(/[^\("]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename" / '"' for opera-bugfix 108 if(tmpImage){ 109 var i = tmpImage.length; 110 while(i--){ // handle baseUrl here for multiple stylesheets in different folders bug 111 var imgSrc = (tmpImage[i].charAt(0) == '/' || tmpImage[i].match('://')) ? // protocol-bug fixed 112 tmpImage[i] : 113 baseURL + tmpImage[i]; 114 115 if(jQuery.inArray(imgSrc, imgUrls) == -1){ 116 imgUrls.push(imgSrc); 117 } 118 } 119 } 120 121 if(!w3cImport && sheets[sheetIndex].imports && sheets[sheetIndex].imports.length) { 122 for(var iImport = 0, importLen = sheets[sheetIndex].imports.length; iImport < importLen; iImport++){ 123 var iHref = sheets[sheetIndex].imports[iImport].href; 124 iHref = iHref.split('/'); 125 iHref.pop(); 126 iHref = iHref.join('/'); 127 if (iHref) { 128 iHref += '/'; //tack on a / if needed 129 } 130 var iSrc = (iHref.charAt(0) == '/' || iHref.match('://')) ? // protocol-bug fixed 131 iHref : 132 baseURL + iHref; 133 134 importedSrc.push(iSrc); 135 imported.push(sheets[sheetIndex].imports[iImport]); 136 } 137 138 139 } 140 }//loop 141 if(imported.length){ 142 parseCSS(imported, importedSrc); 143 return false; 144 } 145 var downloads = settings.simultaneousCacheLoading; 146 while( downloads--){ 147 setTimeout(loadImgs, downloads); 148 } 149 } 150 parseCSS(document.styleSheets); 151 return imgUrls; 152};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.