/** * waitforimages * copyright (c) 2011-2015 alex dickson @alexdickson * https://github.com/alexanderdickson/waitforimages * * licensed under the mit licenses. */ ;(function(factory){if(typeof define==='function'&&define.amd){define(['jquery'],factory);}else if(typeof exports==='object'){module.exports=factory(require('jquery'));}else{factory(jquery);}}(function($){var eventnamespace='waitforimages';$.waitforimages={hasimageproperties:['backgroundimage','liststyleimage','borderimage','bordercornerimage','cursor'],hasimageattributes:['srcset']};$.expr[':']['has-src']=function(obj){return $(obj).is('img[src][src!=""]');};$.expr[':'].uncached=function(obj){if(!$(obj).is(':has-src')){return false;} return!obj.complete;};$.fn.waitforimages=function(){var allimgslength=0;var allimgsloaded=0;var deferred=$.deferred();var finishedcallback;var eachcallback;var waitforall;if($.isplainobject(arguments[0])){waitforall=arguments[0].waitforall;eachcallback=arguments[0].each;finishedcallback=arguments[0].finished;}else{if(arguments.length===1&&$.type(arguments[0])==='boolean'){waitforall=arguments[0];}else{finishedcallback=arguments[0];eachcallback=arguments[1];waitforall=arguments[2];}} finishedcallback=finishedcallback||$.noop;eachcallback=eachcallback||$.noop;waitforall=!!waitforall;if(!$.isfunction(finishedcallback)||!$.isfunction(eachcallback)){throw new typeerror('an invalid callback was supplied.');} this.each(function(){var obj=$(this);var allimgs=[];var hasimgproperties=$.waitforimages.hasimageproperties||[];var hasimageattributes=$.waitforimages.hasimageattributes||[];var matchurl=/url\(\s*(['"]?)(.*?)\1\s*\)/g;if(waitforall){obj.find('*').addback().each(function(){var element=$(this);if(element.is('img:has-src')){allimgs.push({src:element.attr('src'),element:element[0]});} $.each(hasimgproperties,function(i,property){var propertyvalue=element.css(property);var match;if(!propertyvalue){return true;} while(match=matchurl.exec(propertyvalue)){allimgs.push({src:match[2],element:element[0]});}});$.each(hasimageattributes,function(i,attribute){var attributevalue=element.attr(attribute);var attributevalues;if(!attributevalue){return true;} attributevalues=attributevalue.split(',');$.each(attributevalues,function(i,value){value=$.trim(value).split(' ')[0];allimgs.push({src:value,element:element[0]});});});});}else{obj.find('img:has-src').each(function(){allimgs.push({src:this.src,element:this});});} allimgslength=allimgs.length;allimgsloaded=0;if(allimgslength===0){finishedcallback.call(obj[0]);deferred.resolvewith(obj[0]);} $.each(allimgs,function(i,img){var image=new image();var events='load.'+eventnamespace+' error.'+eventnamespace;$(image).one(events,function me(event){var eacharguments=[allimgsloaded,allimgslength,event.type=='load'];allimgsloaded++;eachcallback.apply(img.element,eacharguments);deferred.notifywith(img.element,eacharguments);$(this).off(events,me);if(allimgsloaded==allimgslength){finishedcallback.call(obj[0]);deferred.resolvewith(obj[0]);return false;}});image.src=img.src;});});return deferred.promise();};}));