Skip to content

Instantly share code, notes, and snippets.

@remy
Created September 28, 2012 10:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remy/3799185 to your computer and use it in GitHub Desktop.
Save remy/3799185 to your computer and use it in GitHub Desktop.
Detect whether an iframe is initialised with a width or not - some browser have a width, some don't
var iframedelay = (function () {
var iframedelay = { active : false },
iframe = document.createElement('iframe'),
doc,
callbackName = '__callback' + (+new Date);
iframe.style.height = iframe.style.width = '1px';
iframe.style.visibility = 'hidden';
document.body.appendChild(iframe);
doc = iframe.contentDocument || iframe.contentWindow.document;
window[callbackName] = function (width) {
iframedelay.active = width === 0;
try {
iframe.parentNode.removeChild(iframe);
delete window[callbackName];
} catch (e){};
};
try {
doc.open();
doc.write('<script>window.parent.' + callbackName + '(window.innerWidth)</script>');
doc.close();
} catch (e) {
iframedelay.active = true;
}
return iframedelay;
}());

This script is used to determine whether a dynamically generated iframe should be delayed or not before writing the source.

So you include this code, and a "some point" iframedelay.active is set to true. When your user hits "run code" you create the iframe, and you check this value. If it's true, you need to setTimeout(writeCode, 0) to allow for a reflow.

Arguably, just ditch all this code, and always write in a setTimeout, 0 to be sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment