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

May 22, 2009 at 9:59 AM // reply »
7 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,516 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 »
125 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,516 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
. . . . );


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

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


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 »
125 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
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »
Nov 20, 2009 at 5:07 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I have seen tidbits about the way Railo handles session. I can understand that it lazy-loads sessions, but I also think that I might make some things more complicated. For example, often tim ... read »
Nov 20, 2009 at 4:53 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, you can ramp up the security by turning on J2EE session which gives you a third set of numbers other than CFID/CFTOKEN. There's a reason why ACF put this in place (other than just session replic ... read »