/** * When the CDN support option is enabled the IMG elements src attibutes are replaced with data attributes in the server * side. Then, in the client side, as soon as the DOM is ready, the IMG src attributes are restored with their original * values and an added url parameter with the resolution cookie. This is necessary in cases of CDNs and caching servers * because, without the url parameter, they would cache only one version of each requested image. */ document.addEventListener( 'DOMContentLoaded', function ( event ) { // Make sure there is a String trim function. if ( ! String.prototype.trim ) { String.prototype.trim = function () { return this.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' ); }; } // When the DOM is ready. var cookies = document.cookie.split( ';' ); for ( var k in cookies ) { // Get the plugin resolution cookie. var cookie = cookies[k].trim(); if ( cookie.indexOf( 'resolution' ) === 0 ) { // Restore all images src attribute with the resolution cookie value. var imgs = document.querySelectorAll( 'img[data-adaptive-images-src]' ); for ( var k = 0; k < imgs.length; k++ ) { var img = imgs[k]; var data_src = img.getAttribute( 'data-adaptive-images-src' ); var new_src = data_src.indexOf( '?' ) >= 0 ? data_src + '&' + cookie : data_src + '?' + cookie; img.setAttribute( 'src', new_src ); } // Do the same for the srcset attribute of images and pictures. var pictures = document.querySelectorAll( 'img[data-adaptive-images-srcset], picture source[data-adaptive-images-srcset]' ); for ( var k = 0; k < pictures.length; k++ ) { var picture = pictures[k]; var data_srcset = picture.getAttribute( 'data-adaptive-images-srcset' ); var srcsets = data_srcset.split( ',' ); var new_srcset = []; for ( var m = 0; m < srcsets.length; m++ ) { var srcset = srcsets[m].trim().split( ' ' ); if ( srcset.length > 0 ) { new_srcset.push( srcset[0].indexOf( '?' ) >= 0 ? srcset[0] + '&' + cookie : srcset[0] + '?' + cookie + ' ' + (srcset.length > 1 ? srcset[1] : '') ); } } picture.setAttribute( 'srcset', new_srcset.join( ', ' ) ); } break; } } });