The document Object Methods


The document Object Methods

The most famous document object method is the write method, which enables you to write to a web page. However, this object supports many other methods, as you see in Table 9.4 in overview, and in Table 9.5 in depth.

Table 9.4. Overview of the Methods of the document Object

Methods

   

attachEvent

captureEvents

clear

clearAttributes

close

createAttribute

createComment

createDocumentFragment

createElement

createEventObject

createRange

createStyleSheet

createTextNode

detachEvent

elementFromPoint

execCommand

focus

getElementById

getElementsByName

getElementsByTagName

getSelection

handleEvent

hasFocus

mergeAttributes

open

queryCommandEnabled

queryCommandIndeterm

queryCommandState

queryCommandSupported

queryCommandValue

recalc

releaseCapture

releaseEvents

routeEvent

setActive

write

writeln

   
Table 9.5. The Methods of the document Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

attachEvent

See Chapter 6.

captureEvents

   

x

             
 

Returns: Nothing

 

Lets the window object capture events in Netscape Navigator 4.0. Syntax: captureEvents( eventTypeList ) . Here, eventTypeList holds a list of events to capture, OR'ed together like this: Event.CLICK Event.MOUSEUP . See Chapter 15 for more details.

clear

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

This method clears the current document. Syntax: document.clear() . However, nothing happens after that point, because all elements and scripts are gone. If you really want to rewrite a document, consider using the write method or the innerHTML and innerText properties of the body object instead.

clearAttributes

See Chapter 6.

close

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

This method closes a document that's been opened for writing. Syntax: document.close() . See "Writing to a Document" in this chapter for more details on when to use this method.

createAttribute

     

x

         

x

 

Returns: An attribute object

 

This method creates and returns a new attribute object. Syntax: document.createAttribute( name ) , where name is the text string holding the attribute's name. See "Creating New Elements" in this chapter for an example showing how to create an attribute and connect it to a new element.

createComment

     

x

         

x

 

Returns: A comment object

 

Creates a comment object with the specified data. Syntax: document.createComment( text ) , where text is the text in the comment.

createDocumentFragment

     

x

     

x

x

x

 

Returns: A new document object

 

This method actually creates and returns a whole new document object. Syntax: document.createDocumentFragment() .

createElement

     

x

   

x

x

x

x

 

Returns: A new element object

 

Creates a new HTML element. Syntax: document.createElement( tagName ) , where tagName is the tag name of the new element. In Internet Explorer 4.0, the only new elements you can create are <IMG> , <AREA> ,and <OPTION> . As of Internet Explorer 5.0, you can create all elements programmatically, except for <FRAME> and <IFRAME> . See "Creating New Elements and Nodes" in this chapter for an example showing how to use this method.

createEventObject

               

x

x

 

Returns: An event object

 

Generates an event object for passing event context information when using the fireEvent method (see "The fireEvent Method" in Chapter 6). Syntax: document.createEventObject([ eventObject ]) . Here, eventObject is an existing event object on which to base the new event object.

createRange

     

x

           
 

Returns: A Range object

 

This method creates a Range object, which is the W3C DOM level 2 version of the Internet Explorer's TextRange object. Sytnax: document.createRange() . More on text ranges in Chapter 11, "Working with HTML Text Elements."

createStyleSheet

           

x

x

x

x

 

Returns: A stylesheet object

 

This method creates a new style sheet for the document. Syntax: document.createStyleSheet([ URL ] [, Index ]) , where URL is a string. (If you give a filename for the URL, the style information will be added as a link object; if the URL contains style information, that information is added to the style object). Index is an integer that specifies the index showing where the style sheet should be inserted in the styleSheets collection. When you create a new style sheet, it's added to the page immediately by default, and so becomes active (unlike other methods such as createElement ). See Chapter 21, "Cascading Style Sheets and CGI Programming ," to see how to create style sheets.

createTextNode

     

x

     

x

x

x

 

Returns: A text node object

 

When you create new elements, you build them piece by piece; and to insert text into an element, you create a text node first. Syntax: createTextNode( text ) , where text is the text you want in the node. See "Creating New Elements" in this chapter for an example showing how to use this method.

detachEvent

See Chapter 6.

elementFromPoint

           

x

x

x

x

 

Returns: An element text node

 

This method returns the element at a specific (x, y) point in the document (measured in pixels). Useful for determining what element the mouse is over. Syntax: document.elementFromPoint( X , Y ) . See "Finding Elements by Location" in this chapter for an example.

ExecCommand

           

x

x

x

x

 

Returns: Boolean

 

This method executes a special command using the current document. Syntax: execCommand( Command [, UserInterface ] [, Value ]) , where Command is a string that specifies the command to execute (see "Using execCommand " in this chapter). This command can be any of the command identifiers that can be executed in script. UserInterface is a Boolean that you set to false (the default) if you do not want to display a user interface, and true if you do. Value is a string or number, or other value to assign. This method returns true if successful, false otherwise . See "Using execCommand " in this chapter to learn which commands are possible and to see an example.

focus

See Chapter 6.

getElementById

     

x

     

x

x

x

 

Returns: An element object

 

This method finds an element by ID value, which you set with the ID HTML attribute. Syntax: getElementById( id ) , where id is the ID of the element you're looking for. Discussed in Chapter 4, "Handling the Browser Environment" (see "Accessing HTML Elements" in that chapter).

getElementsByName

     

x

     

x

x

x

 

Returns: An array of element objects

 

This method returns a collection of objects with the same NAME attribute value. Syntax: getElementsByName( name ) , where name is the name of the element(s) you're looking for. Discussed in Chapter 4 (see "Accessing HTML Elements" in that chapter).

getElementsByTagName

See Chapter 6.

getSelection

   

x

x

           
 

Returns: A string

 

Returns any currently selected text in the web page. Syntax: getSelection() . See "Getting Selected Text" in this chapter.

handleEvent

   

x

             
 

This Netscape Navigator 4.0 method handles events captured with setCapture . Syntax: handleEvent( event ) , where event is an event type such as Event.CLICK . See Chapter 15 for more details.

hasFocus

See Chapter 6.

mergeAttributes

See Chapter 6.

open

x

x

x

x

x

x

x

x

x

x

 

Returns: Nothing

 

Opens a document for writing. Syntax: document.open() . See "Writing to a Document" in this chapter to see when to use this method.

queryCommandEnabled

           

x

x

x

x

 

Returns: Boolean

 

Checks whether a special command is enabled. Syntax: queryCommandEnabled( command ) , where command is the command you're checking on. See "Using execCommand " in this chapter to see what commands you can use this method with.

queryCommandIndeterm

           

x

x

x

x

 

Returns: Boolean

 

Checks whether a special command is in the indeterminate state. Syntax: queryCommandIndeterm( command ) , where command is the command you're checking on. See "Using execCommand " in this chapter to see which commands you can use this method with.

queryCommandState

           

x

x

x

x

 

Returns: Boolean

 

Checks whether a special command has been completedreturns true if so. Syntax: queryCommandState( command ) , where command is the command you're checking on. See "Using execCommand " in this chapter to see which commands you can use this method with.

queryCommandSupported

           

x

x

x

x

 

Returns: Boolean

 

Checks whether a special command is supported. Syntax: queryCommandSupported( command ) , where command is the command you're checking on. See "Using execCommand " in this chapter to see which commands you can use this method with.

queryCommandValue

           

x

x

x

x

 

Returns: Strings, numbers

 

Returns the current value of the document, range, or current selection for the given special command. Syntax: queryCommandValue( command ) , where command is the command you're working with. See "Using execCommand " in this chapter to see which commands you can use this method with.

recalc

             

x

x

x

 

Returns: nothing

 

Recalculates all dynamic style properties in the current document. You can set dynamic style properties with the setExpression and getExpression methods. Syntax : document.recalc( [ forceAll ]) , where forceAll is a Boolean that you can set to false (the default) to recalculate only those expressions that have changed since the last recalculation, or true to recalculate all expressions in the document. See Chapter 21 on style sheets for more information.

releaseCapture

See Chapter 6.

releaseEvents

   

x

             
 

Returns: Nothing

 

This Netscape Navigator method releases event capture. Syntax: Syntax: releaseEvents( eventTypeList ) . Here, eventTypeList holds a list of events to release, OR'ed together like this: Event.CLICK Event.MOUSEUP . See Chapter 15 for more details.

routeEvent

   

x

             
 

Returns: Nothing

 

If you've turned on event capture in Netscape Navigator 4.0, this method enables you to route an event after you've worked on the event and want to pass it along to the next event handler. Syntax: routeEvent( event ) . See Chapter 15 for more details.

setActive

See Chapter 6.

write

x

x

x

x

x

x

x

x

x

x

 

Returns: Boolean

 

Writes text to a document. This is the big onesee "Writing to a Document" in this chapter on how to use this method and for examples. Syntax: write( text ) , where text is the text to write to the document.

writeln

x

x

x

x

x

x

x

x

x

x

 

Returns: Boolean

 

Writes text to a document, followed by a carriage return. Syntax: write( text ) , where text is the text to write to the document. See "Writing to a Document" in this chapter.



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