Skip to content

Instantly share code, notes, and snippets.

@jennatollerson
Created October 29, 2020 00:01

Revisions

  1. jennatollerson created this gist Oct 29, 2020.
    37 changes: 37 additions & 0 deletions check_if_pymChild.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // 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();
    }
    };