From 0f5058a248f134781b56bc28c67a75f41495ac80 Mon Sep 17 00:00:00 2001 From: Sarah Goldman Date: Wed, 25 Sep 2013 15:41:10 -0400 Subject: [PATCH 1/3] Reformated test js to pass jslint --- test/jquery.responsiveiframe_test.js | 60 ++++++++++++++-------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/test/jquery.responsiveiframe_test.js b/test/jquery.responsiveiframe_test.js index 129db8c..60e7f63 100644 --- a/test/jquery.responsiveiframe_test.js +++ b/test/jquery.responsiveiframe_test.js @@ -1,9 +1,9 @@ /*global QUnit:false, module:false, test:false, asyncTest:false, expect:false*/ -/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/ +/*global start:false, stop:false, ok:false, equal:false, notEqual:false, deepEqual:false*/ /*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/ -(function($) { - - /* +(function ($) { + 'use strict'; +/* ======== A Handy Little QUnit Reference ======== http://docs.jquery.com/QUnit @@ -22,36 +22,36 @@ raises(block, [expected], [message]) */ - module('jQuery#awesome', { - setup: function() { - this.elems = $('#qunit-fixture').children(); - } - }); + module('jQuery#awesome', { + setup: function () { + this.elems = $('#qunit-fixture').children(); + } + }); - test('is chainable', 1, function() { - // Not a bad test to run on collection methods. - strictEqual(this.elems.awesome(), this.elems, 'should be chaninable'); - }); + test('is chainable', 1, function () { + // Not a bad test to run on collection methods. + strictEqual(this.elems.awesome(), this.elems, 'should be chaninable'); + }); - test('is awesome', 1, function() { - strictEqual(this.elems.awesome().text(), 'awesomeawesomeawesome', 'should be thoroughly awesome'); - }); + test('is awesome', 1, function () { + strictEqual(this.elems.awesome().text(), 'awesomeawesomeawesome', 'should be thoroughly awesome'); + }); - module('jQuery.awesome'); + module('jQuery.awesome'); - test('is awesome', 1, function() { - strictEqual($.awesome(), 'awesome', 'should be thoroughly awesome'); - }); + test('is awesome', 1, function () { + strictEqual($.awesome(), 'awesome', 'should be thoroughly awesome'); + }); - module(':awesome selector', { - setup: function() { - this.elems = $('#qunit-fixture').children(); - } - }); + module(':awesome selector', { + setup: function () { + this.elems = $('#qunit-fixture').children(); + } + }); - test('is awesome', 1, function() { - // Use deepEqual & .get() when comparing jQuery objects. - deepEqual(this.elems.filter(':awesome').get(), this.elems.last().get(), 'knows awesome when it sees it'); - }); + test('is awesome', 1, function () { + // Use deepEqual & .get() when comparing jQuery objects. + deepEqual(this.elems.filter(':awesome').get(), this.elems.last().get(), 'knows awesome when it sees it'); + }); -}(jQuery)); +}(jQuery)); \ No newline at end of file From 9d0c7002297b6b01d5742a548f2285bdac13f8db Mon Sep 17 00:00:00 2001 From: Sarah Goldman Date: Wed, 25 Sep 2013 15:44:36 -0400 Subject: [PATCH 2/3] Added support for multiple iframes on one page by passing the href as well as the height to the parent and then only resizing the iframe with the matching src attribute; Cleaned up the fallback code to both set and check the height hash on the frame. This is still blocked by cross-domain scripting restrictions, however it is not breaking the page in IE7 --- dist/jquery.responsiveiframe.js | 251 ++++++++++++++-------------- dist/jquery.responsiveiframe.min.js | 4 +- src/jquery.responsiveiframe.js | 251 ++++++++++++++-------------- 3 files changed, 258 insertions(+), 248 deletions(-) diff --git a/dist/jquery.responsiveiframe.js b/dist/jquery.responsiveiframe.js index b31cf1e..f748e66 100644 --- a/dist/jquery.responsiveiframe.js +++ b/dist/jquery.responsiveiframe.js @@ -1,142 +1,147 @@ +/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-25 +* https://github.com/npr/responsiveiframe +* Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */ /*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-05 * https://github.com/npr/responsiveiframe * Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */ if (typeof jQuery !== 'undefined') { - (function( $ ){ - var settings = { - xdomain: '*', - ie : navigator.userAgent.toLowerCase().indexOf('msie') > -1, - scrollToTop: true - }; + (function ($) { + 'use strict'; + var settings = { + xdomain: '*', + ie: navigator.userAgent.toLowerCase().indexOf('msie') > -1, + scrollToTop: true + }, + privateMethods = { + messageHandler: function (elem, e) { + var height, r, matches, strD, regex; - var methods = { - // initialization for the parent, the one housing this - init: function() { - return this.each(function(self){ - var $this = $(this); - - if (window.postMessage) { - if (window.addEventListener) { - window.addEventListener('message', function(e) { - privateMethods.messageHandler($this,e); - } , false); - } else if (window.attachEvent) { - window.attachEvent('onmessage', function(e) { - privateMethods.messageHandler($this,e); - }, $this); - } - } else { - setInterval(function () { - var hash = window.location.hash, matches = hash.match(/^#h(\d+)(s?)$/); - if (matches) { - privateMethods.setHeight($this, matches[1]); - if (settings.scrollToTop && matches[2] === 's'){ - scroll(0,0); - } - } - }, 150); - } - }); - } - }; + if (settings.xdomain !== '*') { + regex = new RegExp(settings.xdomain + '$'); + if (e.origin === "null") { + throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server."); + } + if (e.origin.match(regex)) { + matches = true; + } else { + throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain."); + } + } - var privateMethods = { - messageHandler: function (elem, e) { - var height, - r, - matches, - strD; + if (settings.xdomain === '*' || matches) { + strD = String(e.data.split('###')[1]); + r = strD.match(/^(\d+)(s?)$/); + if (r && r.length === 3) { + height = parseInt(r[1], 10); + if (!isNaN(height)) { + try { + privateMethods.setHeight(elem, height); + } catch (ignore) {} + } + if (settings.scrollToTop && r[2] === "s") { + scroll(0, 0); + } + } + } + }, - if (settings.xdomain !== '*') { - var regex = new RegExp(settings.xdomain + '$'); - if(e.origin == "null"){ - throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server."); - } - if(e.origin.match(regex)){ - matches = true; - }else{ - throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain."); - } - - } + // Sets the height of the iframe + setHeight: function (elem, height) { + height = height + 130; + $('iframe[src="' + elem + '"]').css('height', height + 'px'); + }, + getDocHeight: function () { + var D = document; + return Math.min(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight)); + } + }, + methods = { + // initialization for the parent, the one housing this + init: function () { + return this.each(function () { + var $this = $(this); + if (window.postMessage) { + if (window.addEventListener) { + window.addEventListener('message', function (e) { + var elem = e.data.split('###')[0]; + privateMethods.messageHandler(elem, e); + }, false); + } else if (window.attachEvent) { + window.attachEvent('onmessage', function (e) { + var elem = e.data.split('###')[0]; + privateMethods.messageHandler(elem, e); + }, $this); + } + } else { + setInterval(function () { + var elem = $('iframe[src="' + $this[0].src + '"]'), + hash = elem[0].contentWindow.location.hash, + matches; + if (hash) { + matches = hash.match(/^#h(\d+)(s?)$/); + if (matches) { + privateMethods.setHeight($this[0].src, matches[1]); + } + } + }, 150); + } + }); + } + }; - if(settings.xdomain === '*' || matches ) { - strD = e.data + ""; - r = strD.match(/^(\d+)(s?)$/); - if(r && r.length === 3){ - height = parseInt(r[1], 10); - if (!isNaN(height)) { - try { - privateMethods.setHeight(elem, height); - } catch (ex) {} + $.fn.responsiveIframe = function (method) { + if (methods[method]) { + return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } - if (settings.scrollToTop && r[2] === "s"){ - scroll(0,0); + if (typeof method === 'object' || !method) { + $.extend(settings, method); + return methods.init.apply(this); } - } - } - }, + $.error('Method ' + method + ' does not exist on jQuery.responsiveIframe'); + }; + }(jQuery)); +} - // Sets the height of the iframe - setHeight : function (elem, height) { - elem.css('height', height + 'px'); - }, - getDocHeight: function () { - var D = document; - return Math.min( - Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), - Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), - Math.max(D.body.clientHeight, D.documentElement.clientHeight) - ); - } - }; +(function () { + 'use strict'; + var self, + module, + ResponsiveIframe = function () { + self = this; + }; - $.fn.responsiveIframe = function( method ) { - if ( methods[method] ) { - return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); - } else if ( typeof method === 'object' || ! method ) { - $.extend(settings, arguments[0]); - return methods.init.apply( this ); - } else { - $.error( 'Method ' + method + ' does not exist on jQuery.responsiveIframe' ); - } + ResponsiveIframe.prototype.allowResponsiveEmbedding = function () { + if (window.addEventListener) { + window.addEventListener("load", self.messageParent, false); + window.addEventListener("resize", self.messageParent, false); + self.messageParent(); + } else if (window.attachEvent) { + window.attachEvent("onload", self.messageParent); + window.attachEvent("onresize", self.messageParent); + self.messageParent(); + } }; - }( jQuery )); -} -;(function(){ - var self, - module, - ResponsiveIframe = function () {self = this;}; + ResponsiveIframe.prototype.messageParent = function (scrollTop) { + var h = document.body.offsetHeight, + message; + h = scrollTop ? h + 's' : h; + message = window.location.href + '###' + h; + if (top.postMessage) { + top.postMessage(message, '*'); + } else { + window.location.hash = 'h' + h; + } + }; - ResponsiveIframe.prototype.allowResponsiveEmbedding = function() { - if (window.addEventListener) { - window.addEventListener("load", self.messageParent, false); - window.addEventListener("resize", self.messageParent, false); - } else if (window.attachEvent) { - window.attachEvent("onload", self.messageParent); - window.attachEvent("onresize", self.messageParent); + function responsiveIframe() { + return new ResponsiveIframe(); } - }; - ResponsiveIframe.prototype.messageParent = function(scrollTop) { - var h = document.body.offsetHeight; - h = (scrollTop)? h+'s':h; - if(top.postMessage){ - top.postMessage( h , '*'); + // expose + if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { + module.exports.responsiveIframe = responsiveIframe; } else { - window.location.hash = 'h'+h; + window.responsiveIframe = responsiveIframe; } - }; - - function responsiveIframe() { - return new ResponsiveIframe(); - } - - // expose - if ('undefined' === typeof exports) { - window.responsiveIframe = responsiveIframe; - } else { - module.exports.responsiveIframe = responsiveIframe; - } -}()); \ No newline at end of file +}()); diff --git a/dist/jquery.responsiveiframe.min.js b/dist/jquery.responsiveiframe.min.js index 03b23e5..4065c59 100644 --- a/dist/jquery.responsiveiframe.min.js +++ b/dist/jquery.responsiveiframe.min.js @@ -1,4 +1,4 @@ -/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-05 +/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-25 * https://github.com/npr/responsiveiframe * Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */ -"undefined"!=typeof jQuery&&!function(a){var b={xdomain:"*",ie:navigator.userAgent.toLowerCase().indexOf("msie")>-1,scrollToTop:!0},c={init:function(){return this.each(function(){var c=a(this);window.postMessage?window.addEventListener?window.addEventListener("message",function(a){d.messageHandler(c,a)},!1):window.attachEvent&&window.attachEvent("onmessage",function(a){d.messageHandler(c,a)},c):setInterval(function(){var a=window.location.hash,e=a.match(/^#h(\d+)(s?)$/);e&&(d.setHeight(c,e[1]),b.scrollToTop&&"s"===e[2]&&scroll(0,0))},150)})}},d={messageHandler:function(a,c){var e,f,g,h;if("*"!==b.xdomain){var i=new RegExp(b.xdomain+"$");if("null"==c.origin)throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server.");if(!c.origin.match(i))throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain.");g=!0}if(("*"===b.xdomain||g)&&(h=c.data+"",f=h.match(/^(\d+)(s?)$/),f&&3===f.length)){if(e=parseInt(f[1],10),!isNaN(e))try{d.setHeight(a,e)}catch(j){}b.scrollToTop&&"s"===f[2]&&scroll(0,0)}},setHeight:function(a,b){a.css("height",b+"px")},getDocHeight:function(){var a=document;return Math.min(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))}};a.fn.responsiveIframe=function(d){return c[d]?c[d].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof d&&d?(a.error("Method "+d+" does not exist on jQuery.responsiveIframe"),void 0):(a.extend(b,arguments[0]),c.init.apply(this))}}(jQuery),function(){function a(){return new d}var b,c,d=function(){b=this};d.prototype.allowResponsiveEmbedding=function(){window.addEventListener?(window.addEventListener("load",b.messageParent,!1),window.addEventListener("resize",b.messageParent,!1)):window.attachEvent&&(window.attachEvent("onload",b.messageParent),window.attachEvent("onresize",b.messageParent))},d.prototype.messageParent=function(a){var b=document.body.offsetHeight;b=a?b+"s":b,top.postMessage?top.postMessage(b,"*"):window.location.hash="h"+b},"undefined"==typeof exports?window.responsiveIframe=a:c.exports.responsiveIframe=a}(); \ No newline at end of file +"undefined"!=typeof jQuery&&!function(a){"use strict";var b={xdomain:"*",ie:navigator.userAgent.toLowerCase().indexOf("msie")>-1,scrollToTop:!0},c={messageHandler:function(a,d){var e,f,g,h,i;if("*"!==b.xdomain){if(i=new RegExp(b.xdomain+"$"),"null"===d.origin)throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server.");if(!d.origin.match(i))throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain.");g=!0}if(("*"===b.xdomain||g)&&(h=String(d.data.split("###")[1]),f=h.match(/^(\d+)(s?)$/),f&&3===f.length)){if(e=parseInt(f[1],10),!isNaN(e))try{c.setHeight(a,e)}catch(j){}b.scrollToTop&&"s"===f[2]&&scroll(0,0)}},setHeight:function(b,c){c+=130,a('iframe[src="'+b+'"]').css("height",c+"px")},getDocHeight:function(){var a=document;return Math.min(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))}},d={init:function(){return this.each(function(){var b=a(this);window.postMessage?window.addEventListener?window.addEventListener("message",function(a){var b=a.data.split("###")[0];c.messageHandler(b,a)},!1):window.attachEvent&&window.attachEvent("onmessage",function(a){var b=a.data.split("###")[0];c.messageHandler(b,a)},b):setInterval(function(){var d,e=a('iframe[src="'+b[0].src+'"]'),f=e[0].contentWindow.location.hash;f&&(d=f.match(/^#h(\d+)(s?)$/),d&&c.setHeight(b[0].src,d[1]))},150)})}};a.fn.responsiveIframe=function(c){return d[c]?d[c].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof c&&c?(a.error("Method "+c+" does not exist on jQuery.responsiveIframe"),void 0):(a.extend(b,c),d.init.apply(this))}}(jQuery),function(){"use strict";function a(){return new d}var b,c,d=function(){b=this};d.prototype.allowResponsiveEmbedding=function(){window.addEventListener?(window.addEventListener("load",b.messageParent,!1),window.addEventListener("resize",b.messageParent,!1),b.messageParent()):window.attachEvent&&(window.attachEvent("onload",b.messageParent),window.attachEvent("onresize",b.messageParent),b.messageParent())},d.prototype.messageParent=function(a){var b,c=document.body.offsetHeight;c=a?c+"s":c,b=window.location.href+"###"+c,top.postMessage?top.postMessage(b,"*"):window.location.hash="h"+c},"undefined"!=typeof c&&"undefined"!=typeof c.exports?c.exports.responsiveIframe=a:window.responsiveIframe=a}(); \ No newline at end of file diff --git a/src/jquery.responsiveiframe.js b/src/jquery.responsiveiframe.js index 8930970..39c7b90 100644 --- a/src/jquery.responsiveiframe.js +++ b/src/jquery.responsiveiframe.js @@ -1,139 +1,144 @@ +/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-05 +* https://github.com/npr/responsiveiframe +* Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */ if (typeof jQuery !== 'undefined') { - (function( $ ){ - var settings = { - xdomain: '*', - ie : navigator.userAgent.toLowerCase().indexOf('msie') > -1, - scrollToTop: true - }; - - var methods = { - // initialization for the parent, the one housing this - init: function() { - return this.each(function(self){ - var $this = $(this); + (function ($) { + 'use strict'; + var settings = { + xdomain: '*', + ie: navigator.userAgent.toLowerCase().indexOf('msie') > -1, + scrollToTop: true + }, + privateMethods = { + messageHandler: function (elem, e) { + var height, r, matches, strD, regex; - if (window.postMessage) { - if (window.addEventListener) { - window.addEventListener('message', function(e) { - privateMethods.messageHandler($this,e); - } , false); - } else if (window.attachEvent) { - window.attachEvent('onmessage', function(e) { - privateMethods.messageHandler($this,e); - }, $this); - } - } else { - setInterval(function () { - var hash = window.location.hash, matches = hash.match(/^#h(\d+)(s?)$/); - if (matches) { - privateMethods.setHeight($this, matches[1]); - if (settings.scrollToTop && matches[2] === 's'){ - scroll(0,0); - } - } - }, 150); - } - }); - } - }; + if (settings.xdomain !== '*') { + regex = new RegExp(settings.xdomain + '$'); + if (e.origin === "null") { + throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server."); + } + if (e.origin.match(regex)) { + matches = true; + } else { + throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain."); + } + } - var privateMethods = { - messageHandler: function (elem, e) { - var height, - r, - matches, - strD; + if (settings.xdomain === '*' || matches) { + strD = String(e.data.split('###')[1]); + r = strD.match(/^(\d+)(s?)$/); + if (r && r.length === 3) { + height = parseInt(r[1], 10); + if (!isNaN(height)) { + try { + privateMethods.setHeight(elem, height); + } catch (ignore) {} + } + if (settings.scrollToTop && r[2] === "s") { + scroll(0, 0); + } + } + } + }, - if (settings.xdomain !== '*') { - var regex = new RegExp(settings.xdomain + '$'); - if(e.origin == "null"){ - throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server."); - } - if(e.origin.match(regex)){ - matches = true; - }else{ - throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain."); - } - - } + // Sets the height of the iframe + setHeight: function (elem, height) { + height = height + 130; + $('iframe[src="' + elem + '"]').css('height', height + 'px'); + }, + getDocHeight: function () { + var D = document; + return Math.min(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight)); + } + }, + methods = { + // initialization for the parent, the one housing this + init: function () { + return this.each(function () { + var $this = $(this); + if (window.postMessage) { + if (window.addEventListener) { + window.addEventListener('message', function (e) { + var elem = e.data.split('###')[0]; + privateMethods.messageHandler(elem, e); + }, false); + } else if (window.attachEvent) { + window.attachEvent('onmessage', function (e) { + var elem = e.data.split('###')[0]; + privateMethods.messageHandler(elem, e); + }, $this); + } + } else { + setInterval(function () { + var elem = $('iframe[src="' + $this[0].src + '"]'), + hash = elem[0].contentWindow.location.hash, + matches; + if (hash) { + matches = hash.match(/^#h(\d+)(s?)$/); + if (matches) { + privateMethods.setHeight($this[0].src, matches[1]); + } + } + }, 150); + } + }); + } + }; - if(settings.xdomain === '*' || matches ) { - strD = e.data + ""; - r = strD.match(/^(\d+)(s?)$/); - if(r && r.length === 3){ - height = parseInt(r[1], 10); - if (!isNaN(height)) { - try { - privateMethods.setHeight(elem, height); - } catch (ex) {} + $.fn.responsiveIframe = function (method) { + if (methods[method]) { + return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } - if (settings.scrollToTop && r[2] === "s"){ - scroll(0,0); + if (typeof method === 'object' || !method) { + $.extend(settings, method); + return methods.init.apply(this); } - } - } - }, + $.error('Method ' + method + ' does not exist on jQuery.responsiveIframe'); + }; + }(jQuery)); +} - // Sets the height of the iframe - setHeight : function (elem, height) { - elem.css('height', height + 'px'); - }, - getDocHeight: function () { - var D = document; - return Math.min( - Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), - Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), - Math.max(D.body.clientHeight, D.documentElement.clientHeight) - ); - } - }; +(function () { + 'use strict'; + var self, + module, + ResponsiveIframe = function () { + self = this; + }; - $.fn.responsiveIframe = function( method ) { - if ( methods[method] ) { - return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); - } else if ( typeof method === 'object' || ! method ) { - $.extend(settings, arguments[0]); - return methods.init.apply( this ); - } else { - $.error( 'Method ' + method + ' does not exist on jQuery.responsiveIframe' ); - } + ResponsiveIframe.prototype.allowResponsiveEmbedding = function () { + if (window.addEventListener) { + window.addEventListener("load", self.messageParent, false); + window.addEventListener("resize", self.messageParent, false); + self.messageParent(); + } else if (window.attachEvent) { + window.attachEvent("onload", self.messageParent); + window.attachEvent("onresize", self.messageParent); + self.messageParent(); + } }; - }( jQuery )); -} -;(function(){ - var self, - module, - ResponsiveIframe = function () {self = this;}; + ResponsiveIframe.prototype.messageParent = function (scrollTop) { + var h = document.body.offsetHeight, + message; + h = scrollTop ? h + 's' : h; + message = window.location.href + '###' + h; + if (top.postMessage) { + top.postMessage(message, '*'); + } else { + window.location.hash = 'h' + h; + } + }; - ResponsiveIframe.prototype.allowResponsiveEmbedding = function() { - if (window.addEventListener) { - window.addEventListener("load", self.messageParent, false); - window.addEventListener("resize", self.messageParent, false); - } else if (window.attachEvent) { - window.attachEvent("onload", self.messageParent); - window.attachEvent("onresize", self.messageParent); + function responsiveIframe() { + return new ResponsiveIframe(); } - }; - ResponsiveIframe.prototype.messageParent = function(scrollTop) { - var h = document.body.offsetHeight; - h = (scrollTop)? h+'s':h; - if(top.postMessage){ - top.postMessage( h , '*'); + // expose + if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { + module.exports.responsiveIframe = responsiveIframe; } else { - window.location.hash = 'h'+h; + window.responsiveIframe = responsiveIframe; } - }; - - function responsiveIframe() { - return new ResponsiveIframe(); - } - - // expose - if ('undefined' === typeof exports) { - window.responsiveIframe = responsiveIframe; - } else { - module.exports.responsiveIframe = responsiveIframe; - } -}()); \ No newline at end of file +}()); From 09ff4892d00094404624a7e0eb747132f6c45e7b Mon Sep 17 00:00:00 2001 From: Sarah Goldman Date: Thu, 26 Sep 2013 14:53:07 -0400 Subject: [PATCH 3/3] Switched to using an iframe/window name instead of matching location.href to iframe src. This will allow navigation to new pages within a responsive frame to continue to resize as long as new pages also call allow embedding --- dist/jquery.responsiveiframe.js | 10 +++++----- dist/jquery.responsiveiframe.min.js | 4 ++-- src/jquery.responsiveiframe.js | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dist/jquery.responsiveiframe.js b/dist/jquery.responsiveiframe.js index f748e66..cf1d4ac 100644 --- a/dist/jquery.responsiveiframe.js +++ b/dist/jquery.responsiveiframe.js @@ -1,4 +1,4 @@ -/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-25 +/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-26 * https://github.com/npr/responsiveiframe * Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */ /*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-05 @@ -48,7 +48,7 @@ if (typeof jQuery !== 'undefined') { // Sets the height of the iframe setHeight: function (elem, height) { height = height + 130; - $('iframe[src="' + elem + '"]').css('height', height + 'px'); + $('iframe[name="' + elem + '"]').css('height', height + 'px'); }, getDocHeight: function () { var D = document; @@ -74,7 +74,7 @@ if (typeof jQuery !== 'undefined') { } } else { setInterval(function () { - var elem = $('iframe[src="' + $this[0].src + '"]'), + var elem = $('iframe[name="' + $this[0].name + '"]'), hash = elem[0].contentWindow.location.hash, matches; if (hash) { @@ -126,7 +126,7 @@ if (typeof jQuery !== 'undefined') { var h = document.body.offsetHeight, message; h = scrollTop ? h + 's' : h; - message = window.location.href + '###' + h; + message = window.name + '###' + h; if (top.postMessage) { top.postMessage(message, '*'); } else { @@ -144,4 +144,4 @@ if (typeof jQuery !== 'undefined') { } else { window.responsiveIframe = responsiveIframe; } -}()); +}()); \ No newline at end of file diff --git a/dist/jquery.responsiveiframe.min.js b/dist/jquery.responsiveiframe.min.js index 4065c59..6cc0318 100644 --- a/dist/jquery.responsiveiframe.min.js +++ b/dist/jquery.responsiveiframe.min.js @@ -1,4 +1,4 @@ -/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-25 +/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-26 * https://github.com/npr/responsiveiframe * Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */ -"undefined"!=typeof jQuery&&!function(a){"use strict";var b={xdomain:"*",ie:navigator.userAgent.toLowerCase().indexOf("msie")>-1,scrollToTop:!0},c={messageHandler:function(a,d){var e,f,g,h,i;if("*"!==b.xdomain){if(i=new RegExp(b.xdomain+"$"),"null"===d.origin)throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server.");if(!d.origin.match(i))throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain.");g=!0}if(("*"===b.xdomain||g)&&(h=String(d.data.split("###")[1]),f=h.match(/^(\d+)(s?)$/),f&&3===f.length)){if(e=parseInt(f[1],10),!isNaN(e))try{c.setHeight(a,e)}catch(j){}b.scrollToTop&&"s"===f[2]&&scroll(0,0)}},setHeight:function(b,c){c+=130,a('iframe[src="'+b+'"]').css("height",c+"px")},getDocHeight:function(){var a=document;return Math.min(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))}},d={init:function(){return this.each(function(){var b=a(this);window.postMessage?window.addEventListener?window.addEventListener("message",function(a){var b=a.data.split("###")[0];c.messageHandler(b,a)},!1):window.attachEvent&&window.attachEvent("onmessage",function(a){var b=a.data.split("###")[0];c.messageHandler(b,a)},b):setInterval(function(){var d,e=a('iframe[src="'+b[0].src+'"]'),f=e[0].contentWindow.location.hash;f&&(d=f.match(/^#h(\d+)(s?)$/),d&&c.setHeight(b[0].src,d[1]))},150)})}};a.fn.responsiveIframe=function(c){return d[c]?d[c].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof c&&c?(a.error("Method "+c+" does not exist on jQuery.responsiveIframe"),void 0):(a.extend(b,c),d.init.apply(this))}}(jQuery),function(){"use strict";function a(){return new d}var b,c,d=function(){b=this};d.prototype.allowResponsiveEmbedding=function(){window.addEventListener?(window.addEventListener("load",b.messageParent,!1),window.addEventListener("resize",b.messageParent,!1),b.messageParent()):window.attachEvent&&(window.attachEvent("onload",b.messageParent),window.attachEvent("onresize",b.messageParent),b.messageParent())},d.prototype.messageParent=function(a){var b,c=document.body.offsetHeight;c=a?c+"s":c,b=window.location.href+"###"+c,top.postMessage?top.postMessage(b,"*"):window.location.hash="h"+c},"undefined"!=typeof c&&"undefined"!=typeof c.exports?c.exports.responsiveIframe=a:window.responsiveIframe=a}(); \ No newline at end of file +"undefined"!=typeof jQuery&&!function(a){"use strict";var b={xdomain:"*",ie:navigator.userAgent.toLowerCase().indexOf("msie")>-1,scrollToTop:!0},c={messageHandler:function(a,d){var e,f,g,h,i;if("*"!==b.xdomain){if(i=new RegExp(b.xdomain+"$"),"null"===d.origin)throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server.");if(!d.origin.match(i))throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain.");g=!0}if(("*"===b.xdomain||g)&&(h=String(d.data.split("###")[1]),f=h.match(/^(\d+)(s?)$/),f&&3===f.length)){if(e=parseInt(f[1],10),!isNaN(e))try{c.setHeight(a,e)}catch(j){}b.scrollToTop&&"s"===f[2]&&scroll(0,0)}},setHeight:function(b,c){c+=130,a('iframe[name="'+b+'"]').css("height",c+"px")},getDocHeight:function(){var a=document;return Math.min(Math.max(a.body.scrollHeight,a.documentElement.scrollHeight),Math.max(a.body.offsetHeight,a.documentElement.offsetHeight),Math.max(a.body.clientHeight,a.documentElement.clientHeight))}},d={init:function(){return this.each(function(){var b=a(this);window.postMessage?window.addEventListener?window.addEventListener("message",function(a){var b=a.data.split("###")[0];c.messageHandler(b,a)},!1):window.attachEvent&&window.attachEvent("onmessage",function(a){var b=a.data.split("###")[0];c.messageHandler(b,a)},b):setInterval(function(){var d,e=a('iframe[name="'+b[0].name+'"]'),f=e[0].contentWindow.location.hash;f&&(d=f.match(/^#h(\d+)(s?)$/),d&&c.setHeight(b[0].src,d[1]))},150)})}};a.fn.responsiveIframe=function(c){return d[c]?d[c].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof c&&c?(a.error("Method "+c+" does not exist on jQuery.responsiveIframe"),void 0):(a.extend(b,c),d.init.apply(this))}}(jQuery),function(){"use strict";function a(){return new d}var b,c,d=function(){b=this};d.prototype.allowResponsiveEmbedding=function(){window.addEventListener?(window.addEventListener("load",b.messageParent,!1),window.addEventListener("resize",b.messageParent,!1),b.messageParent()):window.attachEvent&&(window.attachEvent("onload",b.messageParent),window.attachEvent("onresize",b.messageParent),b.messageParent())},d.prototype.messageParent=function(a){var b,c=document.body.offsetHeight;c=a?c+"s":c,b=window.name+"###"+c,top.postMessage?top.postMessage(b,"*"):window.location.hash="h"+c},"undefined"!=typeof c&&"undefined"!=typeof c.exports?c.exports.responsiveIframe=a:window.responsiveIframe=a}(); \ No newline at end of file diff --git a/src/jquery.responsiveiframe.js b/src/jquery.responsiveiframe.js index 39c7b90..0bc6d7a 100644 --- a/src/jquery.responsiveiframe.js +++ b/src/jquery.responsiveiframe.js @@ -45,7 +45,7 @@ if (typeof jQuery !== 'undefined') { // Sets the height of the iframe setHeight: function (elem, height) { height = height + 130; - $('iframe[src="' + elem + '"]').css('height', height + 'px'); + $('iframe[name="' + elem + '"]').css('height', height + 'px'); }, getDocHeight: function () { var D = document; @@ -71,7 +71,7 @@ if (typeof jQuery !== 'undefined') { } } else { setInterval(function () { - var elem = $('iframe[src="' + $this[0].src + '"]'), + var elem = $('iframe[name="' + $this[0].name + '"]'), hash = elem[0].contentWindow.location.hash, matches; if (hash) { @@ -123,7 +123,7 @@ if (typeof jQuery !== 'undefined') { var h = document.body.offsetHeight, message; h = scrollTop ? h + 's' : h; - message = window.location.href + '###' + h; + message = window.name + '###' + h; if (top.postMessage) { top.postMessage(message, '*'); } else { @@ -141,4 +141,4 @@ if (typeof jQuery !== 'undefined') { } else { window.responsiveIframe = responsiveIframe; } -}()); +}()); \ No newline at end of file