The Document
object represents the current document being viewed in the browser. As this is a HTML document, made up of various elements (links, forms, etc), then the Document
object is the parent of the various document level elements below it in the hierarchy list.
Properties | Methods | Events |
alinkColor, anchors, applets*, bgColor, cookie, embeds*, fgColor, forms, images*, lastModified, linkColor, links, referrer, title, URL, vlinkColor | open, close, write, writeLn, clear** | OnLoad and OnUnLoad are specified in the <BODY> element, which are actually event handlers for the Window object. |
Netscape (3.0) and above also contains the Applet, Area, Image and Plugin objects as properties of the document object. They are detailed below.
Document Properties
alinkColor
The alinkColor
property reflects the active link colouring, as set by any <BODY ALINK="...">
settings for the current document. It is a writable property, that is, the document active link colour can be set dynamically. For example :
document.alinkColor="#FF0000"
would set the current documents active link colour to red. NOTE : Internet Explorer doesn't support the ALINK
attribute, so while the property may be readable (the author may have set the ALINK
attribute for Netscape users), attempting to dynamically change the active link colour would do nothing.
anchors
The anchors
property is an indexed array containing all the current document's anchors. For more information, see the Anchor Object topic.
applets
The applets
property is an indexed array of all the Java applets contained in a document. For more information, see the Applet Object (below).
bgColor
The bgColor
property reflects the document's background colour, as set by any <BODY BGCOLOR="...">
settings for the current document. It is a writable property, that is, the document background colour can be set dynamically. For example :
document.bgColor="#FFFFE0"
would set the current documents background colour to a light yellow (the colour of this current topic).NOTE : In Internet Explorer, tryng to dynamically alter a documents background colour, if that colour has been set in a style sheet, would not do anything.
cookie
The cookie
property reflects the string value of a cookie accessed, or created by the current document. For more details about the use of cookies, visit http://home.netscape.com/newsref/std/cookie_spec.html
embeds
The embeds
property is an indexed array of any data embedded into a document, to be used by any available plug-in modules. For more information, see the Plug-in Object (below)
fgColor
The fgColor
property reflects the document's foreground colour, as set by any <BODY TEXT="...">
settings for the current document. It is a writable property, that is, the document foreground colour can be set dynamically. For example :
document.fgColor="#0000FF"
would set the current documents foreground (i.e. text) colour to blue. NOTE : In Internet Explorer, trying to dynamically alter a documents text colour, if that colour has been set in a style sheet, would not do anything.
forms
The forms
property is an indexed array of any forms contained in a document. It is itself an object and for more information, see the Form Object topic.
images
The images
property is an indexed array of a documents images. For more information, see the Image Object
lastModified
This property stores the date that the document was last modified (as stored by the web server). It cannot be altered dynamically, but can be read. For example :
document.write ("Last modified : " & document.lastModified)
would insert the document's most recent modification date where the script portion is positioned in the document.
linkColor
The linkColor
property reflects the document's hypertext link colour, as set by any <BODY LINK="...">
settings for the current document. It is a writable property, that is, the document link colour can be set dynamically. For example :
document.linkColor="#008000"
would set the current documents link colour to a dark green. NOTE : In Internet Explorer, trying to dynamically alter a documents text colour, if that colour has been set in a style sheet, would not do anything.
links
The links
property is an indexed array of all the document's links. For more information, see the Link Object.
referrer
This property is a read-only property that contains the URL of the source document that the user navigated from to get to the current document. NOTE : Netscape rightly reports the URL of the source document used to navigate to the current document, but Internet Explorer appears to always return the URL of the current document only.
title
The title
property reflects the document's title, as set in the <TITLE>
element.
URL
The URL
property contains the fully qualified URL of a document and is typically identical to the location.href
property of the Location and Window objects.
vlinkColor
The vlinkColor
property reflects the document's visited link colour, as set by any <BODY VLINK="...">
settings for the current document. It is a writable property, that is, the document visited link colour can be set dynamically. For example :
document.vlinkColor="#400040"
would set the current documents visited link colour to a dark purple (the default colour for visited links). NOTE : In Internet Explorer, trying to dynamically alter a documents text colour, if that colour has been set in a style sheet, would not do anything.
Document Methods
open
The open
method is used to open a data stream, typically followed by write
or writeLn
methods. It can accept a MIME type as a parameter, but by default uses 'text/html', so that standard HTML can be written to the document using the aforementioned write
and writeLn
methods.
close
The close
method is used to close the data stream after a series of document write
or writeLn
method operations. It forces the display of the text/images (any standard HTML) sent during the write
or writeLn
methods.
write
This method can be used to write data (text, or any standard HTML) to a document. For example :
document.write "Hello, I've been scripted into the document"
would write the text string given into the document. It would appear wherever the script that contains the write
method is placed in the document, unless the string to write to the document contains certain formatting data. For example :
document.write "<P ALIGN='right'>Hello, I've been scripted into the document"
would write the same text to the document, but it would be a separate, right-aligned paragraph.
writeLn
The writeLn
method is essentially identical to the write
method, except that it implies a line break after the string to write to the document. As line breaks are ignored by browsers though, the two methods tend to act identically.
clear
This Internet Explorer specific method closes the data stream and updates display of any text/HTML written to the document by write
or writeLn
methods. It is essentially identical to the close
method.
Document Events
The document
object itself has no events, but is affected by the OnLoad
and OnUnLoad
events which can be used in the <BODY>
element (see Scripting the <BODY> element for more information). These event hanlders are actually events of the Window Object.
The Applet Object
The Applet
object (and applets array) are Netscape 3.0 and above (i.e. JavaScript 1.1) specific objects that are also proerties of the document object. Generally, the applets
array would be used for scripting purposes. Any Java applets contained in the document can be referenced either by their name (as set in the <APPLET NAME="...">
attribute), or by their index in the applets array for the document. For example :
MyJavaApplet.name
document.applets[0].name
If, the applet NAME
d MyJavaApplet was the first applet in the document, then the above two code sections would return the same value (i.e. the name of the applet - although it's already known for the first line, as the applet is referenced by name).
The Applet
object contains only one property, (name) which returns the name of the particular applet, and the applets array supports only use of the length property for determining the number of applets in a document.
The Area Object
The Area
object is again, a Netscape 3.0 and above specific property of the document object. Again, it is an indexed array of a documents <AREA>
elements (i.e. client side image maps). Also, to add to the confusion, the area
object is also a property of the links array (see the Link Object) and so contains identical properties to the Link object. As it is an object, any client side image map in the document can be referenced by it's name property (i.e. the NAME
attribute of the <AREA>
element), or by it's position in the links array (see the Link Object). For example :
HeaderMapcircle.href
document.links[0].href
NOTE : The first code section above, implies that in a client side image map definition, one of the hot-spot areas has been named HeaderMapcircle (i.e. <AREA NAME="HeaderMapcircle" ...>
).
The Plugin Object
The Plugin
Object (and plugins array) are actually properties (and at the same time objects) of the Navigator Object. For every <EMBED>
element used in a document, a plugin
object is made (with a corresponding entry in the plugins
and embeds
array). Each entry in the respective arrays represents a plugins object, which has name and description properties, as well as all the MimeTypes properties associated with it. Essentially, these various objects and properties can be used for determining suitable content to present to the user, depending on what plug-ins they have installed with Netscape. For some example code (determining whether the user has a suitable plug-in or not), see the Navigator Object - MimeTypes property.
NOTE : Care should be taken when using Netscape specific scripting objects/properties. As Internet Explorer supports a slightly different JavaScript to Netscape, using properties such as document.link[index]...
may have unexpected results when the document is viewed in Netscape and Internet Explorer. E.g. the first link in the document as displayed by Netscape may be inside a client side image map, but in Explorer, the same reference may refer to a normal hypertext link, which could lead to scripting errors.