IFRAME contentWindow is null

By  on  

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!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    Fullscreen API

    As we move toward more true web applications, our JavaScript APIs are doing their best to keep up.  One very simple but useful new JavaScript API is the Fullscreen API.  The Fullscreen API provides a programmatic way to request fullscreen display from the user, and exit...

  • By
    Create a Brilliant Sprited, CSS-Powered Firefox Animation

    Mozilla recently formally announced Firefox OS and its partners at Mobile World Congress and I couldn't be more excited.  Firefox OS is going to change the lives of people in developing countries, hopefully making a name for itself in the US as well.  The...

Discussion

  1. Sisyphe

    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).

  2. Yes, good catch! Updated!

  3. James

    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.

  4. bell.jackit

    This solution worked for me! Thanks a lot!

  5. Sharry Stowell

    Thank you, saved me hours!

    Sharry

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!