1/* 2 A JavaScript plugin for lazy-loading responsive Google Adsense ads. 3 - 4 By Osvaldas Valutis, www.osvaldas.info 5 Available for use under the MIT License 6*/ 7 8;( function( $, window, document, undefined ) 9{ 10 'use strict'; 11 12 var $win = $( window ), 13 throttle = function(a,b){var c,d;return function(){var e=this,f=arguments,g=+new Date;c&&g<c+a?(clearTimeout(d),d=setTimeout(function(){c=g,b.apply(e,f)},a)):(c=g,b.apply(e,f))}}, 14 15 scriptUrl = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', 16 scriptLoaded = false, 17 throttleTO = 250, 18 19 $adsLoaded = $([]), 20 $adsToLoad = $([]), 21 $adsPending = $([]), 22 23 loadAd = function( $ad ) 24 { 25 ( adsbygoogle = window.adsbygoogle || []).push({}); 26 27 var onLoadFn = $ad.data( 'alOptions' ).onLoad; 28 if( typeof onLoadFn === 'function' ) 29 { 30 $ad.find( 'iframe' ).one( 'load', function() 31 { 32 onLoadFn( $ad ); 33 }); 34 } 35 }, 36 getAdWidth = function( ad ) 37 { 38 return parseInt( window.getComputedStyle( ad, ':before' ).getPropertyValue( 'content' ).slice( 1, -1 ) || 9999 ); 39 }, 40 initAds = function() 41 { 42 if( !$adsToLoad.length ) return true; 43 44 var winScroll = $win.scrollTop(), 45 winHeight = $win.height(); 46 47 $adsToLoad.each( function() 48 { 49 var $this = $( this ), 50 thisOpts = $this.data( 'alOptions' ), 51 laziness = thisOpts.laziness + 1, 52 offset = $this.offset().top; 53 54 // if the element is too far below || too far above 55 if( offset - winScroll > winHeight * laziness || winScroll - offset - $this.outerHeight() - ( winHeight * laziness ) > 0 ) 56 return true; 57 58 $adsToLoad = $adsToLoad.not( $this ); 59 $adsLoaded = $adsLoaded.add( $this ); 60 61 $this 62 .data( 'alOriginalHTML', $this.html()) 63 .data( 'alWidth', getAdWidth( this )) 64 .children( ':first' ) 65 .addClass( 'adsbygoogle' ); 66 67 if( typeof adsbygoogle !== 'undefined' ) loadAd( $this ); 68 else $adsPending = $adsPending.add( $this ); 69 70 if( !scriptLoaded ) 71 { 72 scriptLoaded = true; 73 $.ajax( 74 { 75 url: scriptUrl, 76 async: true, 77 cache: true, 78 dataType: 'script', 79 success: function() 80 { 81 $adsPending.each( function() 82 { 83 loadAd( $( this )); 84 }); 85 $adsPending = $([]); 86 } 87 }); 88 } 89 }); 90 }, 91 resizeAds = function() 92 { 93 if( !$adsLoaded.length ) return true; 94 95 var anyNew = false; 96 $adsLoaded.each( function() 97 { 98 var $this = $( this ); 99 if( $this.data( 'alWidth' ) != getAdWidth( this )) 100 { 101 $adsLoaded = $adsLoaded.not( $this ); 102 $this.html( $this.data( 'alOriginalHTML' )); 103 $adsToLoad = $adsToLoad.add( $this ); 104 anyNew = true; 105 } 106 }); 107 if( anyNew ) initAds(); 108 }; 109 110 $win 111 .on( 'scroll resize', throttle( throttleTO, initAds )) 112 .on( 'resize', throttle( throttleTO, resizeAds )); 113 114 $.fn.adsenseLoader = function( options ) 115 { 116 if( typeof options !== 'string' ) 117 { 118 options = $.extend({}, 119 { 120 laziness: 1, 121 onLoad: false 122 }, 123 options ); 124 } 125 126 this.each( function() 127 { 128 var $this = $( this ); 129 130 if( options === 'destroy' ) 131 { 132 $this.html( $this.data( 'alOriginalHTML' )); 133 $adsToLoad = $adsToLoad.not( $this ); 134 $adsLoaded = $adsLoaded.not( $this ); 135 $adsPending = $adsPending.not( $this ); 136 } 137 else 138 { 139 $this.data( 'alOptions', options ); 140 $adsToLoad = $adsToLoad.add( $this ); 141 } 142 }); 143 144 if( options !== 'destroy' ) 145 initAds(); 146 147 return this; 148 }; 149 150 $.adsenseLoaderConfig = function( options ) 151 { 152 if( typeof options.scriptUrl !== 'undefined' ) 153 scriptUrl = options.scriptUrl; 154 155 if( typeof options.throttle !== 'undefined' ) 156 throttleTO = options.throttle; 157 }; 158 159})( jQuery, window, document );
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.