/** * 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();};}));