Skip to content

Instantly share code, notes, and snippets.

@jennatollerson
Created October 29, 2020 00:01
Show Gist options
  • Save jennatollerson/5c518b9425b787a5fe7bcc490055b95a to your computer and use it in GitHub Desktop.
Save jennatollerson/5c518b9425b787a5fe7bcc490055b95a to your computer and use it in GitHub Desktop.
Check if a page is iframed, add iframe specific enhancements.
// This is meant to be included on child frames created with http://blog.apps.npr.org/pym.js/
// This script checks if our page is iframed, and if so, induces it to cooperate.
// This can allow us to use the same template for all pages on a site, and make being a child iframe an option for any page.
document.addEventListener('DOMContentLoaded', function () {
var inIFrame = (window.location != window.parent.location) ? true : false;
if(inIFrame) {
// If this page is in a iframe...
// ... Load pym.js into this page.
var pymjs = document.createElement('script');
pymjs.src = 'https://pym.nprapps.org/pym.v1.min.js';
document.head.appendChild(pymjs);
pymjs.onload = function () {
// On pym.js load send the height to the parent page.
var pymChild = new pym.Child();
pymChild.sendHeight();
};
// ...add a class we can style against.
document.body.className += ' iframed';
// ...Add target="_parent" to links so they bust out of iframe.
var links = document.getElementsByTagName('a');
var len = links.length;
for(var i=0; i<len; i++) {
if (links[i].target != "_self") {
links[i].target = "_parent";
}
}
}
});
// Simple resize function we can use on the AJAX-y pages.
function pymResize() {
// If we're in an iframe, send a resize event.
if (window.location != window.parent.location) {
var pymChild = new pym.Child();
pymChild.sendHeight();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment