Lecture 16
Lecture 16
Introduction to BOM-DOM
Today’s Lecture
• Browser Object Model (BOM)
– BOM Introduction
– Methods
– Properties
– Events
• Document Object Model (DOM)
– DOM Introduction
– Methods
– Properties
– Events
Browser Object Model (BOM)
• BOM allows JavaScript to “interact with” the browser.
• Default object of browser is window.
• In JavaScript, browser is known as Window Object.
– Window Object represents a window in browser.
– Window Object is supported by all the browsers.
• An object of window is created automatically by the browser.
• window object has several properties and methods that we can use
to interact with it.
• Even document object (of HTML DOM) is a property of window
object.
Window Object Methods
Method Description
alert() Displays an alert box with a message, and OK button.
close() Closes the current window.
confirm() Displays dialog box with a message, OK and Cancel button.
open() Opens a new browser window.
prompt() Displays a dialog box that prompts the visitor for input.
stop() Stops the window from loading.
Window Object Properties
Property Description
console Returns the Console Object for the window.
document Returns the Document object for the window.
innerHeight Returns height of the window's content area (viewport).
innerWidth Returns width of a window's content area (viewport).
Window Events
Attribute Description
onerror Script to be run when an error occurs.
onload Fires after the page is finished loading.
onoffline Script to be run when the browser starts to work offline.
ononline Script to be run when the browser starts to work online.
Document Object Model (DOM)
• DOM is a platform and language independent model to represent
HTML or XML documents.
• When an HTML document is loaded into a web browser, it becomes
a document object.
• In DOM, all parts of the document (i.e., elements, attributes, and
text etc.) are organized in a hierarchical tree (as a family tree in real
life that consists of parents and children).
– In DOM terminology, these individual parts of document are
known as nodes.
– DOM is a collection of nodes:
• Element Nodes, Attribute Nodes, Text Nodes, and Comment
Nodes.
Document Object Model (DOM)
Document Object Model (DOM)
• DOM Tree
<body>
<div>
<p>Paragraph text with a <a href="foo.html">link</a> here.</p>
</div>
</body>
DOM Methods
DOM Methods are actions that we can perform (on HTML Elements).
• Finding HTML Elements by id (getElementById())
• Finding HTML Elements by class name (getElementsByClassName())
• Finding HTML Elements by tag name (getElementsByTagName())
• Finding HTML Elements by CSS selectors
– querySelector()
– querySelectorAll()
• Finding HTML Elements by HTML object collections
(document.forms)
DOM Methods
– Finding HTML Elements by id (getElementById())
• Most common way to access an HTML element is to use id of the
element.
• To find all HTML elements with id, use getElementById().
var myElement = document.getElementById("demo");
– Finding HTML Elements by class name (getElementsByClassName())
• To find all HTML elements with class name, use
getElementsByClassName().
var x = document.getElementsByClassName("demo");
– Finding HTML Elements by tag name (getElementsByTagName())
• To find all HTML elements with tag name, use
getElementsByTagName().
var x = document.getElementsByTagName("p");
DOM Methods
– Finding HTML Elements by CSS selectors (querySelector())
• To find first HTML element that matches a specified CSS selector
(id, class name, type, attribute, value of attribute, etc.), use
querySelector() method.
var x = document.querySelector("p.demo");
– Finding HTML Elements by CSS selectors (querySelectorAll())
• To find all HTML elements that match a specified CSS selector (id,
class name, type, attribute, value of attribute, etc.), use
querySelectorAll() method.
var x = document.querySelectorAll("p.demo");
– Finding HTML Elements by HTML object collections
(document.forms)
const x = document.forms["form1"];
var text = "";
for (var i = 0; i < x.length; i++) {
{ text += x.elements[i].value + "<br>"; }
document.getElementById("demo").innerHTML = text;
DOM Properties
DOM properties are values (of HTML Elements) that we can set or
change.
Property Description
innerHTML Sets or returns the content of an element.
Style Sets or returns the value of style attribute of an element.
Value Get or set the content of input controls.
DOM Events
• Event is an action that can be detected with JavaScript.
• HTML DOM allows JavaScript to react to HTML events.
– In scripts, an event is identified by an event handler.
• Reacting to Events
– A JavaScript can be executed when an event occurs.
– Example of HTML Events are when a user clicks – onclick, when
a web page loaded – onload, and when mouse over an element
– onmouseover, etc.
• HTML Event Attributes
– To assign events to HTML elements, we can use event attributes.