Google Answers Logo
View Question
 
Q: DOM based way to access iframe document object ( No Answer,   2 Comments )
Question  
Subject: DOM based way to access iframe document object
Category: Computers > Internet
Asked by: davious-ga
List Price: $10.00
Posted: 04 Nov 2002 13:17 PST
Expires: 08 Nov 2002 09:28 PST
Question ID: 98572
I want to know if there is a DOM based or standards based way to
access the document object of a frame.  Please provide a breif
explanation and link to the standards documentation.

Also, find code for accessing the frame document
that works on all current browsers (Mac IE5 and up, Windows IE5 and
up, Mozilla 1 and up). I realize this part wouldn't be standards based.

Request for Question Clarification by hammer-ga on 04 Nov 2002 13:28 PST
What language do you want to use?

Clarification of Question by davious-ga on 04 Nov 2002 14:18 PST
javascript
Answer  
There is no answer at this time.

Comments  
Subject: Re: DOM based way to access iframe document object
From: tomg-ga on 06 Nov 2002 17:38 PST
 
There are several different ways of accessing the document of an
iframe, all with varying browser support.

The standard way as defined in DOM 2 is using the contentDocument
property:

  * Official DOM 2 spec
    http://www.w3.org/TR/2002/CR-DOM-Level-2-HTML-20021007/html.html#ID-67133006
  * Mozilla.org reference
    http://www.mozilla.org/docs/dom/domref/dom_frame_ref15.html

So if you give your iframe an id, such as:

  <iframe id="myFrameId" src="page.html"></iframe>

You could then do (presuming the page in the iframe had loaded):

  var frameObj = document.getElementById("myFrameId");
  alert(frameObj.contentDocument.title);

Sadly, Internet Explorer (on both Windows and Mac) doesn't support
this. In fact the only ones that do are Gecko-based ones, such as
Netscape 6, Netscape 7, Mozilla, etc (Gecko is Mozilla's open-source
rendering engine).


So let's have a look at other non-W3C ways of accessing an iframe's
document object. The first and most obvious is the document.frames
array (or "collection" if you speak Microsofteze). First supported by
IE Win and Mac in version 3.02, it is the only way to access an
iframe's document object in IE5.0/win and IE/mac. It also works in
Mozilla/NS6/NS6/etc:

  * Frames collection reference from MSDN
    http://msdn.microsoft.com/workshop/author/dhtml/reference/collections/frames.asp

The way to use it is to give your iframe a name, such as:

  <iframe name="myFrameName" src="page.html"></iframe>

Then in your JS, do:

  var frameObj = document.frames["myFrameName"];
  alert(frameObj.document.title);


Microsoft and Mozilla also support another property called
contentWindow:

  * MSDN contentWindow property reference
    http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/contentwindow.asp
  * Mozilla.org contentWindow reference
    http://www.mozilla.org/docs/dom/domref/dom_frame_ref16.html

You can use it like this:

  <iframe id="myFrameId" src="page.html"></iframe>

  var frameObj = document.getElementById("myFrameId");
  alert(frameObj.contentWindow.document.title);

Please bear in mind that this is a non-standard way of accessing the
document, and has limited support - IE/win 5.5+ and
Mozilla/NS6/NS7/etc only.


So really, the best practical bet for you at the moment is the
document.frames collection. Although not standard, it has by far the
widest browser support. If you want to cover all bases however, use
something like this:

  <iframe id="myFrameId" name="myFrameName" src="page.html"></iframe>

  var frameObj;

  if (document.frames)
    frameObj = document.frames["myFrameName"].document;
   else
    frameObj = document.getElementById("myFrameId").contentDocument;

  alert(frameObj ? frameObj.title : "Sorry, I couldn't find the
iframe.");

That makes sure that if a browser happens to not support the
non-standard document.frames, it can use the contentDocument property
instead. I have to say that's extremely unlikely to happen, no sane
browser is going to drop document.frames support anytime soon.


Some further reading on this subject:

 * http://www.faqts.com/knowledge_base/view.phtml/aid/1797/fid/53/lang
 * http://developer.apple.com/internet/javascript/iframe.html
 * http://www.javaworld.com/jw-04-1996/jw-04-javascript.html


Hope that helps
Tom
Subject: Re: DOM based way to access iframe document object
From: davious-ga on 08 Nov 2002 09:27 PST
 
Thanks very much Tom, you're a pro!  :)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy