IFRAME contentWindow is null
I like clean code so I do what I can to avoid unwanted JavaScript global variables. I initially thought that keys(window)
would give me window
property leaks but that didn't work because browsers returned different results, so I moved on to using an IFRAME
to compare default window property keys.
When I first tried this method, I got a lame error about an IFRAME
element's contentWindow
property being null
. Ugh. It didn't take long to figure out why: you need to wait until the IFRAME
has loaded to get the contentWindow
:
var iframe = document.createElement('iframe');
iframe.onload = function() {
// contentWindow is set!
};
iframe.src = 'about:blank';
document.body.appendChild(iframe);
Of course you'll want to add the onload
event before setting the src
. If you use the load
event to check for the contentWindow
property, you'll be in business!
![Create a CSS Cube]()
CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals. Add animation and you've got something really neat. Unfortunately each CSS cube tutorial I've read is a bit...
![Conquering Impostor Syndrome]()
Two years ago I documented my struggles with Imposter Syndrome and the response was immense. I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions. I've even caught myself reading the post...
![Create a Simple Slideshow Using MooTools]()
One excellent way to add dynamism to any website is to implement a slideshow featuring images or sliding content. Of course there are numerous slideshow plugins available but many of them can be overkill if you want to do simple slideshow without controls or events.
![Get Slick with MooTools Kwicks]()
When I first saw MooTools graphical navigation, I was impressed. I thought it was a very simple yet creative way of using Flash. When I right-clicked and saw that it was JavaScript, I was floored. How could they achieve such...
Don’t you need to append your iframe element to a DOM tree so that the browser fetches its target content ? I mean, I know that old IE will load scripts as you parse an “HTML string” but in modern browsers, I thought that the asset does not get loaded until you append the element to a document (and in my opinion this it what makes constructors such as
Image()
so useful).Yes, good catch! Updated!
Unfortunately this does not appear to be 100% reliable in chrome (i’m currently using version 62.0.3202.94, but this appears to have been an issue for a while), as sometimes contentWindow can still be null when onload is triggered.
This solution worked for me! Thanks a lot!
Thank you, saved me hours!
Sharry