Transcript Ajax, JavaScript and PHP
CNIT 133 Interactive Web Pags – JavaScript and AJAX Document Object Model (DOM)
Agenda
• •
My Web Site: http://fog.ccsf.edu/~hyip (download syllabus, class notes).
DOM object hierarchy.
• • • • •
JavaScript Supports three kinds of objects
Built-in objects: are built in to the JavaScript language. (Date, Array, String) DOM (Document Object Model): objects represent various components of the browser and the current HTML document. Custom objects: objects you created.
NOTE: DOM is not part of the JavaScript language. It is an API (Application Programming Interface) build in to the browser. NOTE: The window object is the parent object for all of the objects. (See next diagram)
The DOM object hierarchy
Window Properties Window Object (Global Object) Window Methods document object (DOM) location object history object (array) navigator object self opener alert() back() blur() clearInterval() clearTimeout() close() confirm() focus() forward() home() open() print() prompt() setInterval() setTimeout() stop() Document Object Collection: images[] forms[] links[] anchors[] Document Object Properties: lastModified title URL Document Object Methods: close() getElementById() getElementsByName() getElementsByTagname() open() write()
HTML Document Object Model (HTML DOM)
Scripting Documents
• • • • • • Every web browser window (or frame) displays an HTML document. The Window object that represents that window has a document property that refers to a Document object.
HTML documents can contain text, images, hyperlinks, form elements, and so on.
JavaScript code can access and manipulate the objects that represent each document element.
A Document Object Model, or DOM, is an API that defines how to access the objects that compose a document. The W3C defines a standard DOM that is reasonably well supported in all modern web browsers.
In the early days, Netscape 2 and 3 supported a simple DOM that provided access only to special document elements such as links, images, and form elements. This legacy DOM was adopted by all browser vendors and has been formally incorporated into the W3C standard as the “Level 0” DOM.
Scripting Documents (continue…)
• • • With IE 4, has a new DOM, it allowed access to all elements in a document. Microsoft’s API is known as the IE 4 DOM. It was never standardized, and IE 5 and later adopted the W3C DOM.
Netscape 4 based on dynamically positioned scriptable elements known as layers. It was supported only in Netscape 4 and dropped from the Mozilla and Firefox browsers.
The W3C DOM standard, or simply, DOM, adopted IE and Netscape DOM.
Dynamic Document Content
• • • Let’s exploring the Document object with its write() method. This method is part of the legacy DOM: You might create a pop-up window and write some HTML to it with code like this: function hello() { var w = window.open(); var d = w.document; d.open(); //open a new document – optional d.write("
Hello world!
"); d.close(); } If you call the write() method on a document that has already been closed, JavaScript implicitly opens a new HTML document.Document Properties
• • Legacy DOM Document object properties: bgColor (deprecated) cookie domain lastModified location referrer title URL You can place code like this to the bottom of each web document:
document:
URL:
Last Update:
• •
Legacy DOM: Document Object Collections
The Document Object Collections, are array-valued properties. They are the heart of the legacy DOM. They are the properties that give you access to certain especial elements of the document: anchors[ ] applets[ ] forms[ ] images[ ] links[ ] Their elements are in the same order as in the document source code. document.forms[0] refers to the first
Assuming that the