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