-
Notifications
You must be signed in to change notification settings - Fork 195
Support for multiple responsive iframes on one page. #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sarahgoldman
wants to merge
3
commits into
npr:master
Choose a base branch
from
RP3Agency:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,142 +1,147 @@ | ||
/*! 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 | ||
* 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[name="' + 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[name="' + $this[0].name + '"]'), | ||
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.name + '###' + 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; | ||
} | ||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am running our demo files in Chrome ( Latest ), and this line is making the iframe grow to huge sizes. I believe this hard code makes the iframe grow, triggering the iframes resize event. This makes an infinite loop causing the the iframe to keep growing.
I removed this hardcoded padding, and it seems to be stable, although the iframe is a bit smaller than the content within it. The scroll bar does show up, which we would like to avoid. I have tried to calculate the padding per browser, but it wasn't stable across browsers. Have any ideas on how this could be better?