1/* 2Jquery Iframe Auto Height Plugin 3Version 1.2.5 (09.10.2013) 4 5Author : Ilker Guller (http://ilkerguller.com) 6 7Description: This plugin can get contents of iframe and set height of iframe automatically. Also it has cross-domain fix (*). 8Details: http://github.com/Sly777/Iframe-Height-Jquery-Plugin 9*/ 10 11(function($){ 12 var uuid = 0; // Unique ID counter for iframes with no ID 13 var iframeOptions = { 14 resizeMaxTry : 4, // how many try that find true values 15 resizeWaitTime : 50, // wait time before next try 16 minimumHeight : 200, // minimum height for iframe 17 defaultHeight : 3000, // default height for iframe 18 heightOffset : 0, // default top offset for iframe 19 exceptPages : "", // Pages that doesnt need auto height 20 debugMode : false, // Debug mode 21 visibilitybeforeload : false, // If you change this to true, iframe will be invisible when every iframe load 22 blockCrossDomain : false, // Set true if you dont want use cross domain fix 23 externalHeightName : "bodyHeight", // Height data name that comes from postMessage (CDI) and gives height value 24 onMessageFunctionName: "getHeight", // Function name that plugin calls this to get data from external source 25 domainName : "*", // Set this if you want to get data from specific domain 26 watcher : false, // Set true if you want to watch iframe document changes automatic 27 watcherTime : 400 // Watcher control Milliseconds 28 }; 29 30 $.iframeHeight = function(el, options){ 31 var base = this; 32 $.iframeHeight.resizeTimeout = null; 33 $.iframeHeight.resizeCount = 0; 34 35 base.$el = $(el); 36 base.el = el; 37 38 base.$el.before("<div id='iframeHeight-Container-" + uuid + "' style='padding: 0; margin: 0; border: none; background-color: transparent;'></div>"); 39 base.$el.appendTo("#iframeHeight-Container-" + uuid); 40 base.$container = $("#iframeHeight-Container-" + uuid); 41 42 base.$el.data("iframeHeight", base); 43 base.watcher = null; 44 45 base.debug = { 46 FirstTime : true, 47 Init : function() { 48 if (!('console' in window)) console={}; 49 'log info warn error dir clear'.replace(/\w+/g,function(f) { 50 if (!(f in console)) console[f]=console.log||new Function; 51 }); 52 }, 53 Log : function(message) { 54 if(this.FirstTime && this.FirstTime === true){ 55 this.Init(); 56 this.FirstTime = false; 57 } 58 if (base.options.debugMode && base.options.debugMode === true && console && (message !== null || message !== "")) { 59 console["log"]("Iframe Plugin : " + message); 60 } 61 }, 62 GetBrowserInfo : (function (pub) { // this function is from Jquery.Migrate with IE6 & Browser Null Fix 63 var matched, browserObj; 64 var uaMatch = function (ua) { 65 ua = ua.toLowerCase(); 66 if (/*@cc_on/*@if(@_jscript_version<=5.6)1@else@*/0/*@end@*/) { 67 ua = "msie 6.0"; 68 } 69 var match = /(chrome)[ \/]([\w.]+)/.exec(ua) || 70 /(webkit)[ \/]([\w.]+)/.exec(ua) || 71 /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || 72 /(msie) ([\w.]+)/.exec(ua) || 73 ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || 74 []; 75 return { 76 browserObj: match[ 1 ] || "", 77 version: match[ 2 ] || "0" 78 }; 79 }; 80 matched = uaMatch(navigator.userAgent); 81 browserObj = { 82 chrome: false, 83 safari: false, 84 mozilla: false, 85 msie: false, 86 webkit: false 87 }; 88 if ( matched.browserObj ) { 89 browserObj[ matched.browserObj ] = true; 90 browserObj.version = matched.version; 91 } 92 93 if (browserObj.chrome) { 94 browserObj.webkit = true; 95 } else if (browserObj.webkit) { 96 browserObj.safari = true; 97 } 98 pub = browserObj; 99 return pub; 100 }(this.GetBrowserInfo || {})) 101 }; 102 103 var isThisCDI = function(){
104 try 105 { 106 var contentHtml; 107 if (base.debug.GetBrowserInfo.msie && base.debug.GetBrowserInfo.version == "7.0") { 108 contentHtml = base.$el.get(0).contentWindow.location.href; 109 } else { 110 contentHtml = base.$el.get(0).contentDocument.location.href; 111 } 112 base.debug.Log("This page is non-Cross Domain - " + contentHtml); 113 return false; 114 } 115 catch(err) 116 { 117 base.debug.Log("This page is Cross Domain"); 118 return true; 119 } 120 }; 121 122 base.resetIframe = function(){ 123 if(base.options.visibilitybeforeload && !(base.debug.GetBrowserInfo.msie && base.debug.GetBrowserInfo.version == "7.0")) base.$el.css("visibility", "hidden"); 124 base.debug.Log("Old Height is " + base.$el.height() + "px"); 125 base.$el.css("height","").removeAttr("height"); 126 base.debug.Log("Reset iframe"); 127 base.debug.Log("Height is " + base.$el.height() + "px after reset"); 128 }; 129 130 base.resizeFromOutside = function(event){ 131 if(base.options.blockCrossDomain) { 132 base.debug.Log("Blocked cross domain fix"); 133 return false; 134 } 135 136 if(typeof event === "undefined") return false; 137 138 if(typeof event.data == "string") { 139 if(event.data == "reset") { 140 base.$el.css("height","").removeAttr("height"); 141 } else { 142 if(!/^ifh*/.test(event.data)) return false; 143 144 if(typeof parseInt(event.data.substring(3)) != "number") return false; 145 var frameHeightPx = parseInt(event.data.substring(3)) + parseInt(base.options.heightOffset); 146 147 base.resetIframe(); 148 base.setIframeHeight(frameHeightPx); 149 } 150 } else { 151 return false; 152 } 153 return true; 154 }; 155 156 base.checkMessageEvent = function(){ // it works on IE8+, Chrome, Firefox3+, Opera and Safari 157 if(base.options.blockCrossDomain || (base.debug.GetBrowserInfo.msie && base.debug.GetBrowserInfo.version == "7.0")) { 158 base.debug.Log("Blocked cross domain fix"); 159 return false; 160 } 161 base.resetIframe(); 162 if(base.options.visibilitybeforeload && !(base.debug.GetBrowserInfo.msie && base.debug.GetBrowserInfo.version == "7.0")) base.$el.css("visibility", "visible"); 163 164 if (window.addEventListener) { 165 window.addEventListener('message', base.resizeFromOutside, false); 166 } else if (window.attachEvent) { 167 window.attachEvent('onmessage', base.resizeFromOutside); 168 } 169 170 171 if (!base.$el.id) { 172 base.$el.id = "iframe-id-" + (++uuid); 173 } 174 var frame = document.getElementById(base.$el.attr("id")); 175 176 var message = base.options.onMessageFunctionName; 177 if (frame.contentWindow.postMessage) { 178 frame.contentWindow.postMessage(message, "*"); 179 } 180 else { 181 base.debug.Log("Your browser does not support the postMessage method!"); 182 return false; 183 } 184 185 base.debug.Log("Cross Domain Iframe started"); 186 return true; 187 }; 188 189 var tryFixIframe = function(){ 190 if($.iframeHeight.resizeCount <= base.options.resizeMaxTry){ 191 $.iframeHeight.resizeCount++; 192 $.iframeHeight.resizeTimeout = setTimeout($.iframeHeight.resizeIframe, base.options.resizeWaitTime); 193 base.debug.Log($.iframeHeight.resizeCount + " time(s) tried"); 194 } else { 195 clearTimeout($.iframeHeight.resizeTimeout); 196 $.iframeHeight.resizeCount = 0; 197 base.debug.Log("set default height for iframe"); 198 base.setIframeHeight(base.options.defaultHeight + base.options.heightOffset); 199 } 200 }; 201 202 base.sendInfotoTop = function(){ 203 if(top.length > 0 && typeof JSON != "undefined"){ 204 var data = {}; 205 data[base.options.externalHeightName].value = $(document).height(); 206 207 var domain = '*'; 208 data = JSON.stringify(data); 209 210 top.postMessage(data, domain); 211 base.debug.Log("sent info to top page"); 212 return false; 213 } 214 return true; 215 }; 216 217 base.setIframeHeight = function(_height) { 218 base.$el.height(_height).css("height", _height); 219 if(base.$el.data("iframeheight") != _height) base.$container.height(_height).css("height", _height); 220 if(base.options.visibilitybeforeload && !(base.debug.GetBrowserInfo.msie && base.debug.GetBrowserInfo.version == "7.0")) base.$el.css("visibility", "visible"); 221 base.debug.Log("Now iframe height is " + _height + "px"); 222 base.$el.data("iframeheight", _height); 223 }; 224 225 $.iframeHeight.resizeIframe = function(){ 226 base.resetIframe(); 227 228 if(isThisCDI()){ 229 base.$el.height(base.options.defaultHeight + base.options.heightOffset).css("height", base.options.defaultHeight + base.options.heightOffset); 230 if(base.options.visibilitybeforeload && !(base.debug.GetBrowserInfo.msie && base.debug.GetBrowserInfo.version == "7.0")) base.$el.css("visibility", "visible"); 231 base.checkMessageEvent(); 232 } else { 233 if(base.$el.css("height") === base.options.minimumHeight+"px") { 234 base.resetIframe(); 235 } 236 237 if(base.$el.get(0).contentWindow.document.body !== null) { 238 base.debug.Log("This page has body info"); 239 var _pageHeight = $(base.$el.get(0).contentWindow.document).height(); 240 var _pageName = base.$el.get(0).contentWindow.document.location.pathname.substring(base.$el.get(0).contentWindow.document.location.pathname.lastIndexOf('/') + 1).toLowerCase(); 241 242 base.debug.Log("page height : " + _pageHeight + "px || page name : " + _pageName); 243 if((_pageHeight <= base.options.minimumHeight && base.options.exceptPages.indexOf(_pageName) == -1)) { 244 tryFixIframe(); 245 } else if (_pageHeight > base.options.minimumHeight && base.options.exceptPages.indexOf(_pageName) == -1) { 246 base.setIframeHeight(_pageHeight + base.options.heightOffset); 247 } 248 } else { 249 base.debug.Log("This page has not body info"); 250 tryFixIframe(); 251 } 252 } 253 }; 254 255 this.$el.bind("updateIframe", function() { 256 $.iframeHeight.resizeIframe(); 257 base.debug.Log("Updated Iframe Manually"); 258 }); 259 260 this.$el.bind("killWatcher", function() { 261 window.clearInterval(base.watcher); 262 base.debug.Log("Killed Watcher"); 263 }); 264 265 base.init = function(){ 266 base.options = $.extend({},$.iframeHeight.defaultOptions, options); 267 if(base.options.watcher == true) base.options.blockCrossDomain = true; 268 269 base.debug.Log(base.options); 270 271 //noinspection JSValidateTypes 272 if(base.$el.get(0).tagName === undefined || base.$el.get(0).tagName.toLowerCase() !== "iframe") { 273 base.debug.Log("This element is not iframe!"); 274 return false;
275 } 276 277 $.iframeHeight.resizeIframe(); 278 base.$el.on('load', function () { 279 $.iframeHeight.resizeIframe(); 280 }); 281 282 if(base.options.watcher) { 283 base.watcher = setInterval(function(){ 284 $.iframeHeight.resizeIframe(); 285 base.debug.Log("Checked Iframe"); 286 }, base.options.watcherTime); 287 } 288 289 return true; 290 }; 291 292 base.init(); 293 }; 294 295 $.iframeHeight.defaultOptions = iframeOptions; 296 297 $.fn.iframeHeight = function(options){ 298 return this.each(function(){ 299 (new $.iframeHeight(this, options)); 300 }); 301 }; 302 303 $.iframeHeightExternal = function (){ 304 if(arguments.length === 1) { 305 if($.isPlainObject(arguments[0])) { 306 iframeOptions = $.extend({}, $.iframeHeight.defaultOptions, arguments[0]); 307 } 308 } 309 310 if (window.addEventListener) { 311 window.addEventListener ("message", OnMessage, false); 312 } 313 else if (window.attachEvent) { 314 window.attachEvent("onmessage", OnMessage); 315 } 316 317 function OnMessage (event){ 318 var _domain; 319 if ('domain' in event) { 320 _domain = event.domain; 321 } 322 if ('origin' in event) { 323 _domain = event.origin; 324 } 325 326 if(iframeOptions.domainName !== "*") { 327 if(_domain !== iframeOptions.domainName) { 328 $.iframeHeight.debug.Log("It's not same domain. Blocked!"); 329 return; 330 } 331 } 332 333 if (event.data == iframeOptions.onMessageFunctionName) { 334 var message = "ifh" + $(document).height(); 335 event.source.postMessage(message, event.origin); 336 } 337 } 338 339 return { 340 update : function() { 341 this.reset(); 342 window.__domainname = iframeOptions.domainName; 343 setTimeout(function(){ 344 var message = "ifh" + $(document).height(); 345 parent.postMessage(message, window.__domainname); 346 }, 90); 347 }, 348 reset : function() { 349 parent.postMessage("reset", iframeOptions.domainName); 350 } 351 }; 352 }; 353})(jQuery);
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.