Working with JavaScript DOM Objects
In this JavaScript tutorial, you will learn about DOM object – window, properties of window object, methods of window object, screen, properties of screen object, navigator, properties of navigator object, methods of navigator object, history, property of history object, methods of history object, location, properties of location object and methods of location object.
DOM stands for Document Object Model. Document Object Model or DOM is a set of objects that allow programmers scripts to integrate with the browser and work with documents, windows, forms, and other components.
The biggest feature that JavaScript has over basic HTML is that scripts can manipulate the web document and its contents. JavaScript can load a new page onto the browser, work with parts of the browser window and document, open new windows, and even modify text dynamically within the page. To work with the browser and documents, JavaScript uses a hierarchy of parent and child objects called the Document Object Model or DOM. These objects are organized into a tree-like structure, and represent all of the content and components of a web document.
All objects in JavaScript have properties and methods associated with them. Document Object Model has properties and methods associated with it. The properties of the Document Object Model are used to describe the web page or document and the methods of the Document Object Model are used for working with parts of the web page. A detailed study of these methods and properties will be made in the coming sections.
The HTML DOM is an abbreviation denoting the Document Object Model for HTML. It is a standard defined by W3C. The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documents. Adding new elements or modifying the contents of the elements using DOM can manipulate HTML elements. HTML DOM is a language independent platform. DOM is technically separate from JavaScript and can be accessed using other scripting languages.
The following list denotes HTML DOM objects that are used for providing information about the environment of the user’s system. These objects are used to provide various information about the user’s environment like screen size, resolution, monitor depth, browser history, the URL, operating system, browser type, version, language and so on. The objects are:
- Window
- Screen
- Navigator
- History
- Location
Associated with each of these objects are properties and methods.
Window:
With respect to the hierarchy of JavaScript, the Window object is the top level object. The Window object denotes the browser window.
The following list gives some of the properties and methods of the Window object.
Properties of Window Object:
- closed
- document
- defaultStatus
- frames
- history
- length
- location
- name
- opener
- parent
- personalbar
- self
- status
- statusbar
- scrollbars
- top
- toolbar
- window
- innerWidth
- innerHeight
- outerWidth
- outerHeight
- pageXOffset
- pageYOffset
- screenX
- screenY
- screenLeft
- screenTop
Methods of Window Object:
- alert()
- blur()
- clearInterval()
- clearTimeout()
- close()
- confirm()
- createPopup()
- focus()
- moveBy()
- moveTo()
- open()
- print()
- prompt()
- resizeBy()
- resizeTo()
- scrollBy()
- scrollTo()
- setInterval()
- setTimeout()
Screen:
Screen object object contains information about the display screen of the client. The object provides information about the user’s video display and color settings. JavaScript runtime engine automatically creates these settings.
There are various properties associated with the Screen object.
Properties of Screen Object:
- availHeight
- availWidth
- bufferDepth
- colorDepth
- deviceXDPI
- deviceYDPI
- fontSmoothingEnabled
- height
- logicalXDPI
- logicalYDPI
- pixelDepth
- updateInterval
- width
In the next sections, the usage, syntax and explanation with examples of each of the properties of the Screen object will be discussed.
Navigator:
Navigator object is an object that is automatically created JavaScript runtime engine. Navigator contains information about the client browser.
This object is used to obtain information about the type of browser being used to access the page. Using this information, the user can be directed to a different page or carry out tasks or functions depending upon the user’s browser.
The following list gives some of the properties and methods of the Navigator object.
Properties of Navigator Object:
-
appCodeName
-
appMinorVersion
-
appName
-
appVersion
-
browserLanguage
-
cookieEnabled
-
cpuClass
-
onLine
-
platform
-
systemLanguage
-
userAgent
-
userLanguage
Methods of Navigator Object:
- javaEnabled()
- taintEnabled()
History:
Like the Screen Object, the History object is also automatically created object by the JavaScript runtime engine. Browsers store the list of the user’s previously visited URLs. Using this list, the user navigates to previously visited web pages using the back and forward buttons in the browser. This list of URLs is called the browser’s history. It’s stored in the form of a list that can be accessed by JavaScript. History object can be assessed by using the window.history property. The History object is a part of a Window object.
The following list gives some of the properties and methods of the History object.
Property of History Object:
- length
Methods of History Object:
- back()
- forward()
- go()
Syntax and explanation with examples of History object properties and methods are discussed in coming sections.
Location:
Location object contains information about the current URL and thus this object provides access and control over the URL of the current window. But one important point to note in this context is that it cannot modify the URL displayed in the browser’s address box and thus adds security in its usage. Since it is part of the Window object it is accessed using the window.location property.
Location Object is automatically created by the JavaScript runtime engine. The following list gives some of the properties and methods of the Location object.
Properties of Location Object:
- hash
- host
- hostname
- href
- pathname
- port
- protocol
- search
Methods of Location Object:
- assign()
- reload()
- replace()