Browser support:
Represents an entire HTML document and provides possibilities for accessing, creating and manipulating all elements in the document.
Syntax:
Properties that reference the object:
+ object .contentDocument
popup .document
window .document
+ object .ownerDocument
Related HTML objects:
a ,
abbr ,
acronym ,
address ,
applet ,
area ,
b ,
base ,
basefont ,
bdo ,
bgsound ,
big ,
blink ,
blockquote ,
body ,
br ,
button ,
caption ,
center ,
cite ,
code ,
col ,
colgroup ,
comment ,
dd ,
del ,
dfn ,
dir ,
div ,
dl ,
dt ,
em ,
embed ,
fieldset ,
font ,
form ,
frame ,
frameset ,
h1 ,
h2 ,
h3 ,
h4 ,
h5 ,
h6 ,
head ,
hr ,
html ,
i ,
iframe ,
img ,
input:button ,
input:checkbox ,
input:file ,
input:hidden ,
input:image ,
input:password ,
input:radio ,
input:range ,
input:reset ,
input:search ,
input:submit ,
input:text ,
ins ,
isindex ,
kbd ,
keygen ,
label ,
legend ,
li ,
link ,
listing ,
map ,
marquee ,
menu ,
meta ,
nobr ,
noframes ,
noscript ,
object ,
ol ,
optgroup ,
option ,
p ,
param ,
plaintext ,
pre ,
q ,
rt ,
ruby ,
s ,
samp ,
script ,
select ,
small ,
span ,
strike ,
strong ,
style ,
sub ,
sup ,
table ,
tbody ,
td ,
textarea ,
tfoot ,
th ,
thead ,
title ,
tr ,
tt ,
u ,
ul ,
var ,
wbr ,
xml ,
xmp
Methods that return the object:
The base interface, through which you can add new functionalities to the
document object, is the
HTMLDocument interface.
Possible members:
Properties
Methods
Events
activeElement
Returns a reference to the object that is currently designated as the active element in the document.
alinkColor
Sets or retrieves the color of active hypertext links in the entire document.
all
Represents a collection of all elements contained by an element or the entire document.
anchors
Represents a collection of all anchor elements in the document that have a name property specified.
applets
Represents a collection of all applet elements in the current document.
attributes
Represents a collection of attribute nodes that belong to an element.
baseURI
Returns the base URL for the object.
bgColor
Sets or retrieves the background color of the document.
body
Specifies the main body of an HTML document.
characterSet
Returns the character encoding of the document .
charset
Sets or retrieves the character encoding of the document, a linked document or a script block.
childNodes
Represents a collection of all nodes that are direct descendants of an element.
compatMode
Returns whether rendering works in Quirks or Strict mode.
contentType
Sets or retrieves the MIME type of the current document.
cookie
Sets or retrieves the cookies stored for the current document.
defaultCharset
Returns the character set that belongs to the current regional and language settings of the operating system.
defaultView
Retrieves a reference to the default AbstractView object for the current document.
designMode
Sets or returns a string value that indicates whether editing is enabled for the document.
dir
Sets or retrieves the horizontal rendering order in the current document.
doctype
Represents a Document Type Definition (DTD).
documentElement
Returns a reference to the root element node of the document.
documentURI
Sets or returns the absolute location of the document.
domain
Sets or returns the hostname of the server that served the current document.
embeds
Represents a collection of all embed elements in the current document.
expando
Sets or returns a Boolean value that indicates whether additional properties should be used within the object.
fgColor
Sets or retrieves the text color for the document.
fileCreatedDate
Returns the date when the file was created.
fileModifiedDate
Returns the date when the file was last modified.
fileSize
Returns the size of the file.
fileUpdatedDate
Returns the date when the file was last updated.
firstChild
Returns a reference to the first child of the current element.
forms
Represents a collection of all form elements in the current document.
frames
Represents a collection of all window objects that belong to frame and iframe elements in the current document.
height
Retrieves the height of the document's contents.
images
Represents a collection of all img elements in the current document.
implementation
Contains information about the features supported by the browser and provides methods for creating base objects.
inputEncoding
Returns the character encoding used for parsing the document.
lastChild
Returns a reference to the last child of the current element.
lastModified
Retrieves the date and time when the document was last modified.
linkColor
Sets or retrieves the color of the hypertext links in the entire document that have not been visited and activated yet.
links
Represents a collection of all anchor and area elements in the current document that have a href property specified.
location
Contains information about the URL of the currently loaded document, and provides methods to navigate to a new page.
mimeType
Retrieves the type of the document file or the type of a linked file.
namespaces
Represents a collection of namespaces that have been declared for custom elements in HTML.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
parentNode
Returns the parent element of the current node in the DOM hierarchy.
parentWindow
Returns a reference to the window object that contains the current document .
plugins
Represents a collection of all embed elements in the current document.
previousSibling
Returns a reference to the previous node of the current element's parent.
protocol
Sets or retrieves the protocol component of the URL assigned to the object.
readyState
Returns a string value that represents the state of the object.
referrer
Retrieves the URL of the document that loaded the current page.
scripts
Represents a collection of all script elements in the current document.
selection
Represents the currently selected part of the document.
styleSheets
Represents a collection of styleSheet objects in the current document.
title
Sets or returns the title of the current document.
uniqueID
Returns the unique identifier generated by the browser for the object.
URL
Specifies or returns the location of the current document.
URLUnencoded
Returns a string value that represents the unencoded version of the location of the current document.
vlinkColor
Sets or retrieves the color of the visited hypertext links in the entire document.
width
Retrieves the width of the browser window's client area.
addEventListener
Registers an event handler function (event listener) for the specified event on the current object.
adoptNode
Moves a node from another document to the current document.
appendChild
Inserts an element after the last child of the current element.
attachEvent
Registers an event handler function (event listener) for the specified event on the current object.
captureEvents
Tells the document or window object to capture the specified types of events.
clear
Clears the entire contents of the currently opened document.
cloneNode
Returns an exact copy of the current node.
close
Closes the output stream opened by the write method and flushes the output buffer to the screen.
compareDocumentPosition
Compares the placement of the specified node with the current node in the DOM hierarchy.
createAttribute
Creates a new attribute node with the specified name.
createAttributeNS
Creates a new attribute node with the specified namespace and name.
createComment
Creates a CommentNode element from the specified text.
createDocumentFragment
Creates an empty DocumentFragment object.
createElement
Creates an element with the specified tag name.
createElementNS
Creates an element with the specified namespace prefix and name.
createEvent
Creates an event object with the specified type.
createEventObject
Creates an event object for message sending.
createNodeIterator
Creates a NodeIterator object that can be used to iterate through the nodes in a subtree.
createNSResolver
Creates an XPathNSResolver object from the specified node that can be used to get namespace URIs from namespace prefixes in the scope of the node.
createRange
Creates an empty Range object.
createStyleSheet
Creates a styleSheet object and inserts it into the current document.
createTextNode
Creates a new TextNode from the specified text.
createTreeWalker
Creates a TreeWalker object that can be used to navigate through the nodes in a subtree.
detachEvent
Removes the specified event handler from the current element that was registered earlier with the attachEvent method.
dispatchEvent
Dispatches the specified event to the current element.
elementFromPoint
Returns the element that is located at the specified coordinates.
evaluate
Evaluates an XPath expression and creates an XPathResult object that represents the result.
execCommand
Allows running commands on certain objects.
focus
Sets the focus on the current element.
getBoxObjectFor
Returns a BoxObject object that represents the bounding rectangle of the specified element in pixels, relative to the upper-left corner of the document.
getElementById
Returns the element with the specified id in the current document.
getElementsByClassName
Returns a NodeList collection that contains all descendant elements of the current element with the specified class name.
getElementsByName
Returns a NodeList collection that contains all elements in the current document with the specified name attribute.
getElementsByTagName
Returns a NodeList collection that contains all descendant elements of the current element with the specified tag name.
getElementsByTagNameNS
Returns a NodeList collection that contains all descendant elements of the current element with the specified namespace URI and local name.
getSelection
Returns the selected content in the current document as a string.
hasAttributes
Returns whether the current element has any attributes specified or not.
hasChildNodes
Returns whether the current node has any child nodes or not.
hasFocus
Returns a Boolean value indicating whether the document or any element in the document has focus.
importNode
Copies a node from another document to the current document.
insertBefore
Inserts an element before the specified child of the current element.
isDefaultNamespace
Returns whether the specified namespace URI is the default namespace in the scope of the current node.
isEqualNode
Returns whether the current node is equal to the specified one.
isSameNode
Returns whether the current node is the same node as the specified one.
isSupported
Returns whether the specified DOM module and version is supported by the current node.
lookupNamespaceURI
Retrieves the namespace URI associated with the specified namespace prefix in the scope of the current node.
lookupPrefix
Retrieves the namespace prefix associated with the specified namespace URI in the scope of the current node.
normalize
Puts the subtree that belongs to the current node into a 'normalized' form.
open
Opens a document stream for writing.
queryCommandEnabled
Returns whether the execution of the specified command can be successful, using the execCommand method.
queryCommandIndeterm
Returns whether the specified command is in an indeterminate state.
queryCommandState
Returns the current state of the specified command.
queryCommandSupported
Returns whether the specified command is supported by the current object.
queryCommandText
Returns the description of the specified command.
queryCommandValue
Returns the actual value of the specified command.
querySelector
Returns a reference to the first descendant element of the current element, that matches the specified selectors.
querySelectorAll
Returns a NodeList collection that contains all descendant elements of the current element that match the specified selectors.
recalc
Recalculates all dynamic properties in the current document.
releaseCapture
Releases the mouse capture from the browser window that was previously set by the setCapture method and restores the normal event processing.
releaseEvents
Stops capturing the specified events that was previously set by the captureEvents method.
removeChild
Removes the specified child node from the current element.
removeEventListener
Removes the specified event handler from the current element that was registered earlier with the addEventListener method.
replaceChild
Replaces the specified child element of the current element with a new element.
write
Writes a string into the document stream.
writeln
Writes a string followed by a new line character into the document stream.
onactivate
Occurs when an element becomes active.
onbeforeactivate
Occurs before an element becomes active.
onbeforecopy
Occurs before the selection is copied to the clipboard and before the oncopy event.
onbeforecut
Occurs before the selection is cut from the document and provides a possibility to enable the Cut menu item.
onbeforedeactivate
Occurs on the active element before it loses the active state.
onbeforeeditfocus
Occurs before an input:file , input:password , input:text or textarea element or an element in an editable region becomes a UI-activated.
onbeforepaste
Occurs before the contents of the clipboard are pasted into the document and provides a possibility to enable the Paste menu item.
onblur
Occurs when an element loses focus.
onclick
Occurs when the user clicks on an element.
oncontextmenu
Occurs when the right mouse button is clicked on an element and the context menu is shown.
oncontrolselect
Occurs before a control is selected in an editable region.
oncopy
Occurs before the selection is copied to the clipboard.
oncut
Occurs before the selection is cut from the document and added to the clipboard.
ondblclick
Occurs when the user double clicks on an element.
ondeactivate
Occurs on the active element when it loses the active state.
ondrag
Occurs periodically on the source element during the drag operation.
ondragend
Occurs on the source element when the user has finished the drag operation.
ondragenter
Occurs on an element when the user moves the mouse pointer into it during a drag operation.
ondragleave
Occurs on an element when the user moves the mouse pointer out of it during a drag operation.
ondragover
Occurs periodically on an element while the mouse pointer is over it during a drag operation.
ondragstart
Occurs on the source element when the user starts the drag operation.
ondrop
Occurs on a possible target element when the dragged data is dropped on it.
onfocus
Occurs when an element receives focus.
onfocusin
Occurs before an element receives focus.
onfocusout
Occurs after an element loses focus.
onhelp
Occurs after the user has pressed the F1 key.
onkeydown
Occurs on an element that has the focus when a key is pressed down and occurs periodically until the key is released.
onkeypress
Occurs on an element that has the focus when a key is pressed down and occurs periodically until the key is released.
onkeyup
Occurs on an element that has the focus when the user releases a key.
onload
Occurs when an object has been loaded.
onmousedown
Occurs when the user presses a mouse button over an element.
onmousemove
Occurs when the user moves the mouse over the element.
onmouseout
Occurs when the user moves the mouse pointer out of the element.
onmouseover
Occurs when the user moves the mouse pointer into the element.
onmouseup
Occurs when the user releases a mouse button over an element.
onmousewheel
Occurs when the mouse wheel rolls.
onoffline
Occurs when the browser starts to work offline.
ononline
Occurs when the browser starts to work online.
onpaste
Occurs before the contents of the clipboard are pasted into the document.
onpropertychange
Occurs every time when the value of an element's property is changed.
onreadystatechange
Occurs when the load state of the data that belongs to an element or a HTML document changes.
onresize
Occurs when the size of an object has changed.
onselectionchange
Occurs when the selection in the document has changed.
onselectstart
Occurs at the start of a selection process.
onstop
Occurs when the user aborts the loading of the document.
Example HTML code 1:
This example illustrates the use of the
document object and the
getElementById method:
< head >
< script type = "text/javascript" >
function GetDivision () {
var div = document . getElementById ( "myDiv" );
alert (div. innerHTML );
}
</ script >
</ head >
< body >
< button onclick = "GetDivision ();" > Get the contents of the division element </ button >
< div id = "myDiv" > A simple division element </ div >
</ body >
Did you find this example helpful?
yes
no
Example HTML code 2:
This example illustrates the use of the
contentDocument property to get the
document object of an
iframe element:
< head >
< script type = "text/javascript" >
var editorDoc;
function InitEditable () {
var editor = document . getElementById ( "editor" );
if (editor. contentDocument )
editorDoc = editor. contentDocument ;
else
editorDoc = editor. contentWindow . document ;
var editorBody = editorDoc. body ;
// turn off spellcheck
if ( 'spellcheck' in editorBody { // Firefox
editorBody. spellcheck = false ;
}
if ( 'contentEditable' in editorBody) {
// allow contentEditable
editorBody. contentEditable = true ;
}
else { // Firefox earlier than version 3
if ( 'designMode' in editorDoc) {
// turn on designMode
editorDoc. designMode = "on" ;
}
}
}
function ToggleBold () {
editorDoc. execCommand ( 'bold' , false , null );
}
</ script >
</ head >
< body onload = "InitEditable ();" >
First, write and select some text in the editor.
< br />
< iframe id = "editor" src = "editable.htm" ></ iframe >
< br />< br />
You can toggle the bold/normal state of the selected text with this button:
< br />
< button onclick = "ToggleBold ();" > Bold </ button >
</ body >
< !DOCTYPE html >
< html >
< head >
< title > Editable content example </ title >
< meta charset = "utf-8" />
</ head >
< body >
Some text in the editor.
</ body >
</ html >
Did you find this example helpful?
yes
no
Related pages:
External links:
Share:
Digg
Del.icio.us
Reddit
Facebook
Twitter
Diigo
User Contributed Comments