Created
April 6, 2018 00:10
-
-
Save didoo/6ec6349d464171c17a9c49e896d85c3f to your computer and use it in GitHub Desktop.
setupIFrame.js
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
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