Skip to content

Instantly share code, notes, and snippets.

@didoo
Created April 6, 2018 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didoo/6ec6349d464171c17a9c49e896d85c3f to your computer and use it in GitHub Desktop.
Save didoo/6ec6349d464171c17a9c49e896d85c3f to your computer and use it in GitHub Desktop.
setupIFrame.js
function setupIFrame(container) {
// create the iframe element
const iFrame = document.createElement('iframe');
iFrame.id = 'styleguide-iframe-' + Math.random().toString(36).substring(2, 10);
iFrame.src = 'about:blank';
iFrame.setAttribute('allowfullscreen', '');
iFrame.setAttribute('frameBorder', '0');
// inject it in the DOM
container.parentNode.insertBefore(iFrame, container);
// store a reference to the iFrame "document" element
const iFrameDocument = iFrame.contentDocument;
// <head>
if (iFrameDocument.head) {
iFrameDocument.head.className = document.head.className;
iFrameDocument.head.innerHTML = document.head.innerHTML;
}
// <body>
if (iFrameDocument.body) {
iFrameDocument.body.className = document.body.className + ' ' + container.className;
// container's children
const children = container.childNodes;
for (let i = 0; i < children.length; i++) {
const cloneChild = iFrameDocument.importNode(children[i], true);
iFrameDocument.body.appendChild(cloneChild);
}
}
// delete the old container
container.remove();
// done
return iFrame;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment