0
3.2kviews
Explain JavaScript objects Window and Document.
1 Answer
0
48views

Window Object

  • The window object represents an open window in a browser.
  • It is a global object, which means that it provides global access to the associated variables and functions.
  • The Window object is the top level or parent object in the hierarchy of browser objects and is automatically defined by the browser and contains in itself several other objects, such as "document", "history" etc.

Window Object Hierarchy

  • The Window object in JavaScript has the following features:
    • Window object collection
    • Window object properties
    • Window object methods

Window Object Collection

  • The Window object collection is a set of all the window objects available in the HTML document.
  • If a document contain frames (<iframe> tags), the browser creates one window object for the HTML document, and one additional window object for each frame.
  • The Window object contains a collection object called frame, which is used to retrieve a collection of window objects defined in the specified document.

Window Object Properties

  • The properties of the Window object are used to retrieve the information about the window that is currently open on the web browser.
  • Properties refer to the variable created inside the Window object. In JavaScript, the available data is attached to the Window object as properties.
  • Syntax used to access Window object properties is as follows:Window.propertyname
    • Closed - Returns a Boolean value indicating whether a window has been closed or not
    • frames - Returns all <iframe> elements in the current window
    • navigator - Returns the Navigator object for the window
    • parent - Returns the parent window of the current window
    • self - Returns the current window
    • name - Sets or returns the name of a window
  • document, defaultstatus, history, length, top, innerheight, outerwidth etc. are some of the properties of Window object

Window Objet Methods

  • Methods are used to perform specific tasks, such as opening, maximizing or minimizing the window.
  • The methods associated with the Window object specify actions, how it displays a message or gets input from the user.
    • alert() - Displays an alert box with a message and an OK button
    • blur() - Removes focus from the current window
    • clearInterval() - Clears a timer set with setInterval()
    • clearTimeout() - Clears a timer set with setTimeout()
    • confirm() - Displays a dialog box with a message and an OK and a Cancel button
    • focus() - Sets focus to the current window
  • close(), open(), print(), prompt(), moveBy(), moveTo(), scrollBy(), resizeTo(), createPopup(), stop() are some of the methods used in Window object.

Document Object

  • When an HTML document is loaded into a web browser, it becomes a document object.
  • The document is a part of the Window object and can be accessed as window.document.
  • The document object is the root node of the HTML document and the "owner" of all other nodes like element nodes, text nodes, attributes nodes, and comment nodes.
  • The document object provides properties and methods to access all node objects, from within JavaScript.
  • Properties of Document object are as follows
    • Body - Sets or returns the document's body
    • Cookie - Returns all name/value pairs of cookies in the document
    • Images -Returns a collection of all <img> elements in the document
    • documentElement - Returns the Document Element of the document
    • documentMode - Returns the mode used by the browser to render the document
    • documentURI - Sets or returns the location of the document
    • domain - Returns the domain name of the server that loaded the document
    • designMode - Controls whether the entire document should be editable or not.
    • Doctype - Returns the Document Type Declaration associated with the document
  • Methods of Document object are as follows
    • addEventListener() - Attaches an event handler to the document
    • adoptNode() - Adopts a node from another document
    • createAttribute() - Creates an attribute node
    • execCommand() - Invokes the specified clipboard operation on the element currently having focus.
    • fullscreenEnabled() - Returns a Boolean value indicating whether the document can be viewed in full screen mode
    • importNode() - Imports a node from another document
  • createComment(), createDocumentFragment(), createElement(), createEvent(), createTextNode(), getElementById(), getElementsByClassName(), getElementsByName(), getElementsByTagName(),renameNode(), removeEventListener(), querySelectorAll(), write(), writeln() are some of the methods used in Document object.
Please log in to add an answer.