Getting IFRAME Window (And Then Document) References With contentWindow

Posted May 22, 2009 at 9:48 AM

Tags: Javascript / DHTML

Perviously, when I had an IFRAME tag on a page and I wanted to get access to the document object within that IFRAME, I would use the top window's "frames" collection:

 Launch code in new window » Download code as text file »

  • window.frames[ "my-frame" ].document

I did this because I could never figure out how to get at the document object directly from the IFRAME reference itself. However, just yesterday, in a comment about my jQuery print() plugin, Todd Rafferty pointed me to an existing print plugin that was able to accomplish this goal. The code, made references to an IFRAME property called "contentWindow". I had never seen this before, but after some Googling, it looks like it was originally an IE property of IFRAMEs that granted access to the "window" of the IFRAME.

Once I have the window object of the IFRAME, I can easily get at the document from it. But, I was nervous that this might be an IE-only property, so I set up the following test using both window-reference methodologies:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Getting IFrame Window Reference With contentWindow</title>
  • <script type="text/javascript" src="jquery-1.3.2.js"></script>
  • <script type="text/javascript">
  •  
  • // When the document has loaded, add an iFrame.
  • $(
  • function(){
  • var jFrame = $( "<iframe name='my-frame'>" );
  • var objDoc = null;
  •  
  • // Set frame properties and add it to the body.
  • jFrame
  • .css( "width", "400px" )
  • .css( "height", "100px" )
  • .appendTo( $( "body" ) )
  • ;
  •  
  • // Now, we are going to use two methods for getting
  • // the iFrame window reference and thereby the
  • // document reference such that we can write to it.
  •  
  • // Use FRAMES array:
  • objDoc = window.frames[ "my-frame" ].document;
  •  
  • objDoc.write( "Gotten via FRAMES <br />" );
  •  
  • // Use the contentWindow property of the iFrame.
  • objDoc = jFrame[ 0 ].contentWindow.document;
  •  
  • objDoc.write( "Gotten via contentWindow <br />" );
  •  
  • // Close the document.
  • objDoc.close();
  • }
  • );
  •  
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Getting IFrame Window Reference With contentWindow
  • </h1>
  •  
  • <p>
  • An iFrame will be added below this:
  • </p>
  •  
  • </body>
  • </html>

When running this code, I got the following output on FireFox, IE (6,7), Safari, and Chrome:

 
 
 
 
 
 
Getting IFRAME Window And Document References With ContentWindow. 
 
 
 

Rock on! It looks like contentWindow returns the window reference of the IFRAME on all relevant browsers. That's an awesome property. Thanks Todd!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Ryan Mcilmoyl
May 22, 2009 at 9:59 AM // reply »
6 Comments

Ben,
There's also a contentDocument property, but not as widely supported (IE doesn't support it, although it's part of the W3C standard). In the past, I've used something like
var iframeDocument = iframeObject.contentDocument ? iframeObject.contentDocument : iframeObject.contentWindow.document;
Having said that, referencing the frames collection works pretty much everywhere, and as your tests show, I think all the modern browsers implement contentWindow.


May 22, 2009 at 10:26 AM // reply »
6,371 Comments

@Ryan,

Yeah, I came across contentDocument in my Googling, but as you are saying, it's not widely supported. I think getting the window reference is a nice route as you need the window reference a lot as well.

It would be nice if they all supported contentDocument, but until then :)


May 22, 2009 at 2:43 PM // reply »
123 Comments

@Ben

http://scheduler.cfunited.com/js/scheduler.js

If you search for "getIFrameDocument" that's what I used. It works in basically every browser, new and old. :)


May 25, 2009 at 10:20 AM // reply »
6,371 Comments

@Elliott,

Looks pretty solid. For funzies, I think you could turn this into short-circuit evaluation:

return(
. . . . iframe.contentDocument ||
. . . . iframe.contentWindow.document ||
. . . . iframe.document
. . . . );


Stefano
Jun 9, 2009 at 1:39 PM // reply »
1 Comments

You rock men, i've been looking for this solution for hours ;)
Thank you


Muhammad Asif
Jun 10, 2009 at 4:39 AM // reply »
1 Comments

Can I use the iframe to fill up forms on another website. For example my task is to open websites with filled up forms. What can I do to do this?

In fact I want to open other websites in iframe and want to use javascript to hold the document inside iframe and to fill up forms inside it.


Jun 10, 2009 at 4:56 PM // reply »
123 Comments

@Muhammad

You can only interact with the document of an iframe if the document it loaded is on the same domain.

So it would depend on what you meant by "other websites".


Post Comment  |  Ask Ben

Recent Blog Comments
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »
Nov 6, 2009 at 4:53 PM
How To Unformat Your Code (Like A Pro)
I tried to go *back* the other way. Adding formatting is actually a much more complicated problem than removing formatting. Anyway, here is what I could put together with a minimal amount of time: ... read »
Asaf
Nov 6, 2009 at 2:35 PM
ColdFusion GetPageContext() Massive Exploration
Hi, I actually found this post useful. I recently acquired a SSL certificate for my website and when I switched over to HTTPS Internet Explorer would throw an error when trying to download a dynamic ... read »
Nov 6, 2009 at 2:19 PM
How To Unformat Your Code (Like A Pro)
@Chuck, @Nathan, Well, now I feel like it's a challenge.... I accept. ... read »
Nathan Stanford
Nov 6, 2009 at 2:06 PM
How To Unformat Your Code (Like A Pro)
@Chuck, I would love it as well. I was not joking I am very serious I love well formatted code. ... read »
Chuck
Nov 6, 2009 at 1:54 PM
How To Unformat Your Code (Like A Pro)
Ben great job, I happen to like your coding style and find it very easy to understand and follow. @Nathan, I totally agree, I (like the rest of you I assume) am pretty meticulous when it comes to c ... read »