Download presentation
Presentation is loading. Please wait.
Published byMyrtle Lee Modified over 9 years ago
1
CHAPTER 11 USING THE DOCUMENT OBJECT MODEL
2
LEARNING OBJECTIVES How by using events such as mouseover or mouseout, your pages can respond to specific user mouse operations How using an object’s innerHTML attribute allows you to access the nested HTML tags because many HTML tags enclose other HTML tags How using the Window object allows you to use JavaScript code within your page to access specifics about the current window, such as its size or relative screen position How using the Navigator object allows you to use JavaScript code to determine the Web browser that is displaying the page contents How using the Screen object allows you to use JavaScript code within your page to determine specifics about the current screen resolution How using the Location object allows you to use JavaScript code within your page to determine specifics about the current web-page, such as its URL, file path, or query string
3
DOM OVERVIEW AT: HTTP://WWW.W3.ORG/DOM/
4
UNDERSTANDING OBJECTS An object, in the simplest sense, is a “thing.” With respect to an HTML page, an object might be a paragraph, image, or table. All things have attributes, and the same is true for DOM objects. A paragraph, for example, has related text; an image has a corresponding source URL; and a table might have a height and a width. In addition to attributes, the browser associates different events with objects, such as a mouse entering a table, hovering over an image, and so on. The sections that follow use different JavaScript functions to leverage object attributes and in the process change a webpage’s behavior.
5
USING DOM TO CHANGE AN IMAGE function imageDog() { var ImageObject = document.getElementById("DogCat"); ImageObject.src = "http://www.websitedevelopmentbook.com/Chapter11/Dog.jpg"; } function imageCat() { var ImageObject = document.getElementById("DogCat"); ImageObject.src = "http://www.websitedevelopmentbook.com/Chapter11/Cat.jpg"; }
6
USING DOM TO CHANGE TEXT COLORS function greenText() { var paragraphObject = document.getElementById("Paragraph"); paragraphObject.style.color = 'yellow'; } function blueText() { var paragraphObject = document.getElementById("Paragraph"); paragraphObject.style.color = 'blue'; } Paragraph text
7
DETECTING A WINDOW-RESIZE EVENT
8
ACCESSING AN ELEMENT’S INNERHTML Paragraph one Paragraph two Paragraph three Show innerHTML
9
CHANGING AN ELEMENT’S INNERHTML function toHindi() { var paragraphObject = document.getElementById("Paragraph"); paragraphObject.innerHTML = " गर्भावस्थाक आमतौर पर 9 महीयने (40 सप्ताैह ) तक चलती है, जिसे आपकी पिछली माहवारी की तारीख से गिना जाता है। अपनी सटीक नियत तिथि का अनुमान लगाने के लिए ( लगभग 2 सप्ताकह तक सटीक ) अपनी पिछली माहवारी की तारीख से आरंभ करें और उसमें 280 दिन "; } function toEnglish() { var paragraphObject = document.getElementById("Paragraph"); paragraphObject.innerHTML = "Pregnancy normally lasts 9 months (40 weeks), calculated from the date of your last menstrual period. To estimate (accurate to about 2 weeks) your due date, start with the date of your last menstrual cycle and then add 280 days."; } Pregnancy normally lasts 9 months (40 weeks), calculated from the date of your last menstrual period. To estimate (accurate to about 2 weeks) your due date, start with the date of your last menstrual cycle and then add 280 days.
10
WRITING CONTENT TO THE CURRENT DOCUMENT document.writeln("Hello, DOM"); Other document content
11
WRITING HTML TO THE CURRENT DOCUMENT document.writeln(" Hello, DOM"); Other document content
12
DISPLAYING WINDOW SPECIFICS alert("Top: " + window.screenY + " Window.left: " + window.screenX + " Inner Height: " + window.innerHeight + " Inner Width: " + window.innerWidth);
13
PRINTING THE CURRENT WINDOW’S CONTENTS Print Window
14
DISPLAYING SCREEN SPECIFICS alert("Width: " + screen.width + " Height: " + screen.height + " Colors: " + screen.colorDepth);
15
DISPLAYING THE CURRENT BROWSER NAME alert(navigator.appName);
16
USING THE HISTORY OBJECT function changeContent() { var value = document.getElementById('PullDown').value; history.go(value); } for (i = 0; i " + i + " "); }
17
DISPLAYING CURRENT PAGE INFORMATION alert("Host name: " + location.host + " Path: " + location.pathname + " Protocol: " + location.protocol);
18
REAL WORLD: W3SCHOOLS
19
SUMMARY To render a webpage, a browser treats the elements that make up the page, such as paragraphs, headings, images, lists, tables, and so, as objects. In the simplest sense, an object is a thing consisting of attributes, events, and operations. One object may be a paragraph, one an image, one a table, and so on. To access such objects within a webpage, browsers use the Document Object Model, or DOM. This chapter introduced the Document Object Model. Using JavaScript within your webpage, you can use DOM to access individual page components to create dynamic page content that your page can change based on operations the user performs. You can also use DOM objects to perform key operations, such as validating a form’s contents.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.