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 |