Document object

Objective

  • Use the document object to access document properties and methods

  • MDN Document object documentation
  • w3schools.com Document object documentation
  • The document object has many useful properties such as:
    • activeElement: a reference to the element in the document which currently has focus
    • anchors: a list of all the anchor elements in the document
    • body: a reference to the document's body element
    • cookie: access to the document's cookies
    • designMode: gets and sets the ability to make the whole document editable
    • forms: a list of all the form elements in the document
    • head: a reference to the head element of the document
    • images: a list of all the image elements in the document
    • location: the URI of the document
    • readyState: the loading status of the document
    • referrer: the URI of the page that linked to this one
    • scripts: a list of all the script elements in the document
    • title: sets or gets the title of the document
  • The document object has many useful event handlers such as:
    • onblur: rasied when the document loses focus
    • onfocus: rasied when the document gets focus
    • onkeydown: used for detecting keys being typed
    • onkeypress: used for detecting keys being typed
    • onkeyup: used for detecting keys being typed
    • onload: used to detect when a document is finished loading
    • onmousedown: used to detect when a mouse button is pressed
    • onmouseup: used to detect when a mouse button is released
    • onmousemove: used to detect when the mouse moves in the document area
    • onmouseover: used to detect when the mouse enters the document area
    • onmouseout: used to detect when the mouse leaves the document area
    • onsubmit: used to detect when a form is being submitted
  • The document object has many useful methods for manipulating the DOM:
    • open: opens a document so it can be written to
    • close: closes a document to signal writing is done
    • write: outputs a string to the document
    • writeln: outputs a string to the document followed by a newline
    • hasFocus: indicates whether the document has focus
  • The document object has many useful methods for working with the DOM:
    • createElement: creates a new element given a tag name
    • createComment: creates a new HTML comment
    • createAttribute: creates a new Attr object
    • createTextNode: creates a new text node
    • getElementsByClassName: returns a list of elements with the specified class name
    • getElementsByTagName: returns a list of elements with the specified tag name
    • getElementById: returns the element with the specified id (or null if none found)