1/*jslint white: true, indent: 2, onevar: false, browser: true, undef: true, nomen: false, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, strict: false, newcap: true, immed: true */ 2/*global window, console, jQuery, setTimeout */ 3 4/* 5 Plugin: iframe autoheight jQuery Plugin 6 Version: 1.9.5 7 Author and Contributors 8 ======================================== 9 NATHAN SMITH (http://sonspring.com/) 10 Jesse House (https://github.com/house9) 11 aaron manela (https://github.com/aaronmanela) 12 Hideki Abe (https://github.com/hideki-a) 13 Patrick Clark (https://github.com/hellopat) 14 ChristineP2 (https://github.com/ChristineP2) 15 Mmjavellana (https://github.com/Mmjavellana) 16 yiqing-95 (https://github.com/yiqing-95) 17 jcaspian (https://github.com/jcaspian) 18 adamjgray (https://github.com/adamjgray) 19 Jens Bissinger (https://github.com/dpree) 20 jbreton (https://github.com/jbreton) 21 mindmelting (https://github.com/mindmelting) 22 23 File: jquery.iframe-auto-height.plugin.js 24 Remarks: original code from http://sonspring.com/journal/jquery-iframe-sizing 25 Description: when the page loads set the height of an iframe based on the height of its contents 26 see README: http://github.com/house9/jquery-iframe-auto-height 27 28*/ 29(function ($) { 30 $.fn.iframeAutoHeight = function (spec) { 31 32 var undef; 33 if ($.browser === undef) { 34 var message = []; 35 message.push("WARNING: you appear to be using a newer version of jquery which does not support the $.browser variable."); 36 message.push("The jQuery iframe auto height plugin relies heavly on the $.browser features."); 37 message.push("Install jquery-browser: https://raw.github.com/house9/jquery-iframe-auto-height/master/release/jquery.browser.js"); 38 alert(message.join("\n")); 39 return $; 40 } 41 42 // set default option values 43 var options = $.extend({ 44 heightOffset: 0, 45 minHeight: 0, 46 maxHeight: 0, 47 callback: function (newHeight) {}, 48 animate: false, 49 debug: false, 50 diagnostics: false, // used for development only 51 resetToMinHeight: false, 52 triggerFunctions: [], 53 heightCalculationOverrides: [] 54 }, spec); 55 56 // logging 57 function debug(message) { 58 if (options.debug && options.debug === true && window.console) { 59 console.log(message); 60 } 61 } 62 63 // not used by production code 64 function showDiagnostics(iframe, calledFrom) { 65 debug("Diagnostics from '" + calledFrom + "'"); 66 try { 67 debug(" " + $(iframe, window.top.document).contents().find('body')[0].scrollHeight + " for ...find('body')[0].scrollHeight"); 68 debug(" " + $(iframe.contentWindow.document).height() + " for ...contentWindow.document).height()"); 69 debug(" " + $(iframe.contentWindow.document.body).height() + " for ...contentWindow.document.body).height()"); 70 } catch (ex) { 71 // ie fails when called during for each, ok later on 72 // probably not an issue if called in a document ready block 73 debug(" unable to check in this state"); 74 } 75 debug("End diagnostics -> results vary by browser and when diagnostics are requested"); 76 } 77 78 // show all option values 79 debug(options); 80 81 // ****************************************************** 82 // iterate over the matched elements passed to the plugin ; return will make it chainable 83 return this.each(function () { 84 85 // ****************************************************** 86 // http://api.jquery.com/jQuery.browser/ 87 var strategyKeys = ['webkit', 'mozilla', 'msie', 'opera']; 88 var strategies = {}; 89 strategies['default'] = function (iframe, $iframeBody, options, browser) { 90 // NOTE: this is how the plugin determines the iframe height, override if you need custom 91 return $iframeBody[0].scrollHeight + options.heightOffset; 92 }; 93 94 jQuery.each(strategyKeys, function (index, value) { 95 // use the default strategy for all browsers, can be overridden if desired 96 strategies[value] = strategies['default']; 97 }); 98 99 // override strategies if registered in options 100 jQuery.each(options.heightCalculationOverrides, function (index, value) { 101 strategies[value.browser] = value.calculation; 102 }); 103 104 function findStrategy(browser) { 105 var strategy = null; 106 107 jQuery.each(strategyKeys, function (index, value) { 108 if (browser[value]) { 109 strategy = strategies[value]; 110 return false;
111 } 112 }); 113 114 if (strategy === null) { 115 strategy = strategies['default']; 116 } 117 118 return strategy; 119 } 120 // ****************************************************** 121 122 // for use by webkit only 123 var loadCounter = 0; 124 125 var iframeDoc = this.contentDocument || this.contentWindow.document; 126 127 // resizeHeight 128 function resizeHeight(iframe) { 129 if (options.diagnostics) { 130 showDiagnostics(iframe, "resizeHeight"); 131 } 132 133 // set the iframe size to minHeight so it'll get smaller on resizes in FF and IE 134 if (options.resetToMinHeight && options.resetToMinHeight === true) { 135 iframe.style.height = options.minHeight + 'px'; 136 } 137 138 // get the iframe body height and set inline style to that plus a little 139 var $body = $(iframe, window.top.document).contents().find('body'); 140 var strategy = findStrategy($.browser); 141 var newHeight = strategy(iframe, $body, options, $.browser); 142 debug(newHeight); 143 144 if (newHeight < options.minHeight) { 145 debug("new height is less than minHeight"); 146 newHeight = options.minHeight; 147 } 148 149 if (options.maxHeight > 0 && newHeight > options.maxHeight) { 150 debug("new height is greater than maxHeight"); 151 newHeight = options.maxHeight; 152 } 153 154 newHeight += options.heightOffset; 155 156 debug("New Height: " + newHeight); 157 if (options.animate) { 158 $(iframe).animate({height: newHeight + 'px'}, {duration: 500}); 159 } else { 160 iframe.style.height = newHeight + 'px'; 161 } 162 163 options.callback.apply($(iframe), [{newFrameHeight: newHeight}]); 164 } // END resizeHeight 165 166 // debug me 167 debug(this); 168 if (options.diagnostics) { 169 showDiagnostics(this, "each iframe"); 170 } 171 172 // if trigger functions are registered, invoke them 173 if (options.triggerFunctions.length > 0) { 174 debug(options.triggerFunctions.length + " trigger Functions"); 175 for (var i = 0; i < options.triggerFunctions.length; i++) { 176 options.triggerFunctions[i](resizeHeight, this); 177 } 178 } 179 180 // Check if browser is Webkit (Safari/Chrome) or Opera 181 if ($.browser.webkit || $.browser.opera || $.browser.chrome) { 182 debug("browser is webkit or opera"); 183 184 // Start timer when loaded. 185 $(this).load(function () { 186 var delay = 0; 187 var iframe = this; 188 189 var delayedResize = function () { 190 resizeHeight(iframe); 191 }; 192 193 if (loadCounter === 0) { 194 // delay the first one 195 delay = 500; 196 } else { 197 // Reset iframe height to 0 to force new frame size to fit window properly 198 // this is only an issue when going from large to small iframe, not executed on page load 199 iframe.style.height = options.minHeight + 'px'; 200 } 201 202 debug("load delay: " + delay); 203 setTimeout(delayedResize, delay); 204 loadCounter++; 205 }); 206 207 // Safari and Opera need a kick-start. 208 var source = $(this).attr('src'); 209 $(this).attr('src', ''); 210 $(this).attr('src', source); 211 } else { 212 // For other browsers. 213 if(iframeDoc.readyState === 'complete') { 214 resizeHeight(this); 215 } else { 216 $(this).load(function () { 217 resizeHeight(this); 218 }); 219 } 220 } // if browser 221 222 }); // $(this).each(function () { 223 }; // $.fn.iframeAutoHeight = function (options) { 224}(jQuery)); // (function ($) {
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.