[Javascript] writing to iframe

Andrew Clover and-babble at doxdesk.com
Fri Apr 1 13:10:01 CST 2005


John Huebner <JHuebner08 at student.mvcc.edu> wrote:

> Iframe_name.document.open;
> Iframe_name.document.clear;
> Iframe_name.documentl.write("content to be written");
> Iframe_name.document.close;

open, clear and close are methods too, so if you want to call them 
(rather than just refer to them) you have to say open(), clear() and 
close(). You shouldn't need document.clear() anyway.

Avoid writing just 'Iframe_name' - this requires that the browser copies 
references to named items as properties of the 'window' object (which 
acts like a global scope for a web page script). Most browsers *will* do 
this - not least because IE and Moz make 'window.frames' a synonym for 
'window' - but it's best not to rely on it.

You should also ensure that the iframe src points to a document on the 
same hostname, so that you're allowed to access its 'document' property.

> I've even tried to call the iframe using

> window.frames("Iframe_name").document

Nearly.

window.frames is supposed to be accessed like an Array (in some browsers 
it might actually be one), so use square instead of round brackets to 
access things from it.

By the way in JavaScript, property and member access are the same thing, so:

   window.frames['Iframe_name'].document

is the same as saying:

   window.frames.Iframe_name.document

You only need the brackets if the name is not a valid identifier (ie. it 
has funny characters in) or if you want to use a variable to hold the 
property name you're going to access.

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/



More information about the Javascript mailing list