The document Object Properties


The document Object Properties

We've worked with the document object ever since Chapter 1, "Essential JavaScript," and we'll take a look at this object's properties here. In Chapter 3, "The JavaScript Language: Loops, Functions, and Errors," we took a look at this code (Listing 03-03.html on the web site) that loops over all the properties of the document object and displays them:

 <HTML>      <HEAD>          <TITLE>              Using the for...in Loop          </TITLE>      </HEAD>      <BODY>          <H1>Using the for...in Loop</H1>          <SCRIPT LANGUAGE="JavaScript">              <!--  for (var property in document) {   document.write(property + ": " + document[property] + "<BR>")   }  // -->          </SCRIPT>      </BODY>  </HTML> 

You can see the results of this code in Chapter 3it's Figure 3.3 for the Netscape Navigator and in Figure 3.4 for the Internet Explorer. In this chapter, it's time to take a look at these properties in detail. You can see the document object properties in overview in Table 9.1, and in depth in Table 9.2.

Table 9.1. Overview of the Properties of the document Object

Properties

   

activeElement

alinkColor

all

anchors

applets

attributes

bgColor

body

charset

characterSet

childNodes

cookie

defaultCharset

designMode

doctype

documentElement

domain

embeds

expando

fgColor

fileCreatedDate

fileModifiedDate

fileSize

firstChild

forms

frames

height

ids

images

implementation

lastChild

lastModified

layers

linkColor

links

location

mimeType

namespaces

namespaceURI

nextSibling

nodeName

nodeType

ownerDocument

parentNode

parentWindow

plugins

previousSibling

protocol

readyState

referrer

scripts

security

selection

styleSheets

tags

title

uniqueID

URL

URLUnencoded

vlinkColor

width

   
Table 9.2. The Properties of the document Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

activeElement

           

x

x

x

x

 

Read-only

 

This property holds the "active element," which either has the focus or will get the focus when the focus is returned to the present window. Useful if you have a multipurpose function that needs to retrieve data from the control the user is working with in another window.

alinkColor

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Holds the active link color in the web page. Set to a valid HTML colora color triplet or a predefined browser color such as " magenta ." See "Setting Colors" in this chapter.

all

See Chapter 5.

anchors

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

Holds the <A> element objects in the web page in an array as JavaScript a objects. See "The document Object Collections" in this chapter.

applets

 

x

x

x

   

x

x

x

x

 

Read-only

 

Holds the applets as specified in <APPLET> elements in the web page in an array of JavaScript applet objects. See "The document Object Collections" in this chapter.

attributes

See Chapter 5.

bgColor

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Holds the background color of the web page. Set to a valid HTML colora color triplet or a predefined browser color such as "cyan." See "Setting Colors" in this chapter.

body

     

x

   

x

x

x

x

 

Read/write

 

Refers to the <BODY> element, as covered later in this chapter. This property holds a JavaScript body object and gives you a handy way to gain access to that object, without needing to assign a name or ID to the <BODY> element first.

charset

           

x

x

x

x

 

Read/write

 

Holds the name of the character set used by the Internet Explorer as a string. In the HTML 4.01 specification, the W3C says that you can get a list of the registered character sets at ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets.

characterSet

     

x

           
 

Read/write

 

Holds the name of the character set used by the Netscape Navigator as a string. In the HTML 4.01 specification, the W3C says that you can get a list of the registered character sets at ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets.

childNodes

See Chapter 5.

cookie

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Enables you to create and access cookies. This is a cool onesee "Creating Cookies" in this chapter for more details, as well as the in-depth coverage in Chapter 23, "Cookies and Creating Your Own Objects."

defaultCharset

           

x

x

x

x

 

Read/write

 

Holds the name of the default character set used by the Internet Explorer as a string. In the HTML 4.01 specification, the W3C says that you can get a list of the registered character sets at ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets. You can use this property to restore the charSet property if that property has been changed.

designMode

             

x

x

x

 

Read/write

 

Sets or retrieves a value that indicates whether the document can be edited. It's a string that may be set to "On" to indicate that the document can be edited, or "Off Inherit" (the default) to indicate that the document cannot be edited.

doctype

     

x

         

x

 

Read-only

 

Holds an object specifying the document type information for the document. This property is used more frequently in XML documents; for HTML documents in both the Netscape Navigator and Internet Explorer, it is set to null.

documentElement

     

x

     

x

x

x

 

Read-only

 

This property returns the root node of a documentfor HTML documents, that's the <HTML> element object itself, and this property gives you access to that element object.

domain

 

x

x

x

 

x

x

x

x

x

 

Read/write

 

This property starts off holding the host name of the server (as a string) from which the page came. If a site has multiple servers, the site can prevent access between frames that were filled from different servers as a security precaution. However, you can assign a domain suffix to allow sharing of pages between frames with this property. For example, a page in one frame from users.starpowder.com and a page from www.starpowder.com might not be able to communicate with each other; by setting both page's domain property to " starpowder.com ," however, both pages are treated as secure and each can access the other.

embeds

 

x

x

x

   

x

x

x

x

 

Read-only

 

Holds an array of the <EMBED> elements in a page as embed objects, which has the same properties as the <EMBED> elements have attributes. This element is often used to display or play multimedia files. See "The document Object Collections" in this chapter.

expando

           

x

x

x

x

 

Read/write

 

This Boolean property indicates whether the document object can support custom ("expando") properties. If set to true, you can store and retrieve data in custom properties like this: document.userNumber = 5 . If false, custom properties are ignored.

fgColor

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Holds the foreground (text) color of the web page. Set to a valid HTML colora color triplet or a predefined browser color such as "blue." See "Setting Colors" in this chapter.

fileCreatedDate

           

x

x

x

x

 

Read-only

 

Holds the date the file was created as a string. (Don't count on this property too heavily; many servers don't give accurate information here.) See "Handling File Dates and Sizes" in this chapter.

fileModifiedDate

           

x

x

x

x

 

Read-only

 

Holds the date the file was last modified as a string. (Don't count on this property too heavily; many servers don't give accurate information here.) See "Handling File Dates and Sizes" in this chapter.

fileSize

           

x

x

x

x

 

Read-only

 

Holds the file size (in bytes) as an integer. (Don't count on this property too heavily; many servers don't give accurate information here.) See "Handling File Dates and Sizes" in this chapter.

firstChild

See Chapter 5.

forms

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

Holds an array of form objects corresponding to the <FORM> elements in the page. You can use to access controls in a form like this: document.forms[1].text1.value . See "The document Object Collections" in this chapter.

frames

           

x

x

x

x

 

Read-only

 

Holds an array of the frame objects corresponding to the <FRAME> elements in the page. Each item in the array is really the window object in the frame, so to access the document of a particular frame, you can use document.frames[2].document . See "The document Object Collections" in this chapter. (Personally, I stick with the window.frames property, because it feels a little odd to think of a <FRAMESET> page as containing a document.)

height

   

x

x

           
 

Read-only

 

Holds the height of the document in the browser's window. Holds values measured in pixels.

ids

   

x

             
 

Read-only

 

This Netscape Navigator 4.0-only property was used in this browser's JavaScript-based style sheet syntax, and functions something like the Internet Explorer's all collection. However, JavaScript-based style sheets have largely been replaced with CSS style sheets.

images

 

x

x

x

   

x

x

x

x

 

Read-only

 

Holds an array of the <IMG> elements in the page as img objects. For example, you can access an image's src property like this: document.images[5].src . See "The document Object Collections" in this chapter, and see Chapter 15, "Working with the Mouse, Keyboard, and Images," for more on image handling.

implementation

     

x

         

x

 

This property holds an implementation object that gives some indication of the HTML and/or XML standards supported by the browser. The implementation object supports no properties and only one method, hasFeature , which returns true if the browser supports a particular W3C feature and false otherwise . To check for W3C HTML DOM level 2 support, for example, you can call document.implementation.hasFeature ("HTML," "2.0"). Currently, the features you can check on in the Netscape Navigator 6.0 are "HTML," "XML," "Views," "Stylesheets," "CSS," "Events," "MouseEvents," "HTMLEvents," and "Range." The level you can check on can only be "1.0" or "2.0." In the Internet Explorer, you can check these features only: "HTML" and "XML;" and the level argument is optional and currently only arguments set to "1.0" can return a value of true.

lastChild

See Chapter 5.

lastModified

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

Returns the date the document was last modified, as a string. Not Internet Explorer-only like fileModifiedDate .

layers

   

x

             
 

Read-only

 

Holds the layers in a document in an array of layer objects. See Chapter 16, "Dynamic HTML: Changing Web Pages On-the-Fly," for more on layers and "The document Object Collections" in this chapter.

linkColor

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Holds the color for unvisited links in the web page. Set to a valid HTML colora color triplet or a predefined browser color such as "purple." See "Setting Colors" in this chapter.

links

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

Holds an array of the links in the document (as link objects)that is, <A> elements with HREF attributes (unlike the anchors collection, which holds all the <A> elements). See "The document Object Collections" in this chapter.

location

 

x

x

x

   

x

x

x

x

 

Read/write

 

Holds the URL of the web page as a string. This is not the same as the location object (which is window.location , not document.location )! Due to the confusion ( especially if you just use location without qualifying it as window.location or document.location ), this property is being phased out in favor of document.URL .

mimeType

             

x

x

x

 

Read-only

 

Supposed to indicate the MIME (Multipurpose Internet Mail Extension) type of a document as a string, but in practice very unreliable. May be better supported in the future as XML becomes more important.

namespaces

               

x

x

 

Read-only

Holds an array of all namespaces declared in a document as namespace objects. Largely for use with Internet Explorer behaviors. See "The document Object Collections" in this chapter, and see Chapter 17, "Dynamic HTML: Drag and Drop, Data Binding, and Behaviors," for more on behaviors.

namespaceURI

See Chapter 5.

nextSibling

See Chapter 5.

nodeName

See Chapter 5.

nodeType

See Chapter 5.

ownerDocument

See Chapter 5.

parentNode

See Chapter 5.

parentWindow

           

x

x

x

x

 

Read-only

Holds the window object that contains the current document. In other words, document.parentWindow is the same as just referring to the window object.

plugins

   

x

x

   

x

x

x

x

 

Read-only

The same as the embeds propertyholds an array of the <EMBED> elements in a page as embed objects (which have the same properties as <EMBED> elements have attributes). This element is often used to display or play multimedia files. This property seems to be used less and less these days plugin is a Netscape Navigator term , and is slowly coming to be replaced by embed in the standards.

previousSibling

See Chapter 5.

protocol

           

x

x

x

x

 

Read/write

Holds a string that contains the full name of the protocol used to retrieve the document. Documents on the Internet report "Hypertext Transfer Protocol" are here; local files on the same machine report "File Protocol."

readyState

See Chapter 5.

referrer

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

Holds the URL (as a string) of the web page in which the user clicked a link to get to the current page. (The user must have clicked a link for this property to be filled.) Note that some programmers consider this property is problematic in the Windows version of Internet Explorer 3.0 and 4.0. See "Tracking User Navigation" in this chapter.

scripts

           

x

x

x

x

 

Read-only

 

Holds an array of the <SCRIPT> elements in the browser, as script objects. See "The document Object Collections" in this chapter.

security

               

x

x

 

Read-only

 

This is actually an undocumented feature of the Internet Explorer (undocumented in both Internet Explorer 5.5 and 6.0), and it seems it'll get more play in the future. For now, the only return value seems to be the string: "This type of document does not have a security certificate."

selection

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

This property holds the current text selection (as selected by the user) in the page as a selection object. See "Getting Selected Text" in this chapter for an example showing how to use this property.

styleSheets

     

x

   

x

x

x

x

 

Read-only

 

Holds the style sheets in the page as an array of styleSheet objects. See "The document Object Collections" in this chapter.

tags

   

x

             
 

Read-only

 

This Netscape Navigator 4.0-only property is used with the JavaScripttype style sheets available in this browser, and it works much like the getElementsByTagName method. However, JavaScript-type style sheets are little used today, having been supplanted by CSS style sheets. See "The document Object Collections" in this chapter.

title

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Holds the document's title (from the <TITLE> element) as a string. In Internet Explorer 4.0 and Netscape Navigator, this property became read/write; before that, it was read-only.

uniqueID

See Chapter 5.

URL

 

x

x

x

   

x

x

x

x

 

Read/write

 

Holds the document's URL as a string. Note that this property returns an encoded URL stringfor example, spaces are replaced with %20 (the hexadecimal character code for a space) so that browsers can treat the URL as one unbroken string. To get the unencoded URL, use the URLUnencoded property.

URLUnencoded

               

x

x

 

Read-only

 

Holds the document's URL as an unencoded string (which means, for example, that spaces will be spaces and not represented as the hexadecimal character code %20).

vlinkColor

x

x

x

x

x

x

x

x

x

x

 

Read/write

 

Holds the color for visited links in the web page. Set to a valid HTML colora color triplet or a predefined browser color such as "yellow." See "Setting Colors" in this chapter.

width

   

x

x

           
 

Read-only

 

Holds the width of the document in the browser's window. Holds values measured in pixels.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net