JavaScript - Document Object Model. Bhawna Mallick 2 Unit Plan What is DOM and its use? Hierarchy of DOM objects. Reflection of these objects in an HTML.

Slides:



Advertisements
Similar presentations
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
Lesson 3 HTML Forms Handling. the Form object Tag : Properties: –action - action attribute of tag –elements[ ] - creeated from like named form elements.
JavaScript Forms Form Validation Cookies CGI Programs.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Information Technology Center Hany Abdelwahab Computer Specialist.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
CST JavaScript Validating Form Data with JavaScript.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
JavaScript Form Validation
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
JavaScript and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
JavaScript - a Brief Introduction Anupriya. What is JavaScript Object based (not object oriented) programming language –very limited object creation –a.
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
1 CS101 Introduction to Computing Lecture 18 Objects, Properties, Methods (Web Development Lecture 6)
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: JavaScript & Forms.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
1 rfXcel Confidential Copyright 2007 Web Technology JavaScript 12/10/07.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
LOGO FORMs in HTML CHAPTER 5 Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
Introduction to JavaScript Objects, Properties, Methods.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
© 2007 D. J. ForemanJS-1 A Light Introduction for Non-programmers JavaScript for Beginners.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
HTML Forms.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Java Script This is a first JavaScript example. This is a first JavaScript example.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Ashima Wadhwa Java Script And Forms. Introduction Forms: –One of the most common Web page elements used with JavaScript –Typical forms you may encounter.
Chapter 4 Java Script – Part2. 2 Location object Properties Properties href – whole path will be displayed. ex: window.location.href ( see example 11)
Chapter 5 Validating Form Data with JavaScript
JavaScript and HTML Simple Event Handling 11-May-18.
In this session, you will learn to:
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
JavaScript and HTML Simple Event Handling 19-Sep-18.
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
JavaScript and Forms Kevin Harville.
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JavaScript - Document Object Model

Bhawna Mallick 2 Unit Plan What is DOM and its use? Hierarchy of DOM objects. Reflection of these objects in an HTML page. Some of the important objects, their methods and properties Form validation and advantages

Bhawna Mallick 3 What is DOM? DOM stands for the Document Object Model. The World Wide Web Consortium (W3C) is the body which sets standards for the web. W3C has defined a standard Document Object Model, known as the W3C DOM for accessing HTML elements from a browser. It views HTML documents as a tree structure of elements and text embedded within other elements. DOM stands apart from JavaScript because other scripting languages could access it. All the browsers are following DOM and because of this the JavaScript code will work on all the browsers in the same way. XML DOM and Browser DOM are different.

Bhawna Mallick 4 Hierarchy of Objects Window Frame document Location Layer History Link Image Area Anchor Applet Plugin Form Password Hidden Submit Reset Radio Checkbox Button Select FileUpload Text Text area Option

Bhawna Mallick 5 How to Access username using DOM Username Password document object refers the html document.( tag) Inside the document we have a form object Inside form we have two textboxes According to the DOM we can access the value inside the textbox using JavaScript like document.userlogin.username.value In an form we are accessing elements by name. The name should be unique.

Bhawna Mallick 6 DOM Top Level objects ObjectHas Description window methods properties The window object is the top level object in the DOM. It contains information about the window and the frames document methods properties collections The document object represents the HTML document, and is used to access the HTML elements inside the document navigator methods properties Contains information about the version of Navigator in use. History Methods Properties Contains information on URLs that the user has visited in a window.

Bhawna Mallick 7 Navigator object The navigator object refers to the browser itself. Has properties only.You cannot modify properties. All of the properties of the navigator object are read-only. appCodeNameThe code name of the browser appNameThe name of the browser. appVersionA string that represents version information about the browser. userAgentThe value of the user-agent header sent in the HTTP protocol from client to server. platformindicates the platform (Windows, UNIX, and so on)

Bhawna Mallick 8 Window Object The window object is the "parent" object for all other objects Enables to access the browser window or the frame within it. Some methods are  alert( message)  prompt( message[, inputDefault])  confirm( message)  moveTo( x-coordinate, y-coordinate)  open( URL, windowName[, windowFeatures])  close()  status property

Bhawna Mallick 9 History Object Contains information on URLs that the user has visited in a window Provides methods to revisit those URLs Properties :  length:Reflects the number of entries in the history list. Methods :  back():Loads the previous URL in the history list.  forward():Loads the next URL in the history list.  go(int ):Loads a URL from the history list. Eg: history.go(-2) // goes back to the URL the user visited two clicks ago in the current window.

Bhawna Mallick 10 Location Object Contains information on the current URL. Properties hostA string specifying the server name, subdomain, and domain name. hostnameA string containing the full hostname of the server hrefA string specifying the entire URL. protocolA string specifying the beginning of the URL, up to and including the first colon. Methods reload()Forces a reload of the window’s current document replace(URL)Loads the specified URL over the current history entry. The properties will be displayed properly only when you are accessing the html page from a web server.

Bhawna Mallick 11 Document Object Each page has only one document object. Contains information about the current document, and provides methods for displaying HTML output to the user. write(string) Writes one or more HTML expressions to a document in the specified window. writeln(string ) Writes one or more HTML expressions to a document in the specified window and follows them with a new line character. URLA string that specifies the complete URL of a document. titleA string that specifies the contents of the TITLE tag.

Bhawna Mallick 12 Document Object Properties lastModified A string that specifies the date the document was last modified. alinkColor linkColor vlinkColor A string that specifies the ALINK attribute. A string that specifies the LINK attribute. A string that specifies the VLINK attribute. fgColor bgColor A string that specifies the TEXT attribute. A string that specifies the BGCOLOR attribute. forms An array a containing an entry for each form in the document. height, widthHeight and width of the browser

Bhawna Mallick 13 Form Object Each form in a document creates a form object. A document can have more than one form Form objects in a document are stored in a forms[ ] collection. The elements on a form are stored in an elements[ ] array. Each form element has a form property that is a reference to the element’s parent form. Yes No To access the first radio button value you can use document.forms[0].elements[0].value (or) document.form1.rad1[0].value // radio buttons must have the //same name in a group, so this will act as a control array

Bhawna Mallick 14 Form Object Properties actionReflects the ACTION attribute. elementsAn array reflecting all the elements in a form. lengthReflects the number of elements on a form. methodReflects the METHOD attribute. targetReflects the TARGET attribute. Methods reset()Resets a form. submit()Submits a form. Event Handlers  onReset,() onSubmit()

Bhawna Mallick 15 Text, Textarea, Password, hidden Objects Properties  defaultValue: Reflects the VALUE attribute.  name : NAME attribute.  type: Reflects the TYPE attribute.  value: Reflects the current value of the Text object’s field. Methods  focus(): Gives focus to the object.  blur(): Removes focus from the object.  select(): Selects the input area of the object. Event Handler  onBlur: when a form element loses focus  onChange: field loses focus and its value has been modified.  onFocus: when a form element receives input focus.  onSelect: when a user selects some of the text within a text field.

Bhawna Mallick 16 Radio object Properties  name, type, value, defaultChecked, defaultvalue, checked  checked property will have a Boolean value specifying the selection state of a radio button. (true/false) Methods  click( ), focus( ), blur( ) Event Handler  onClick, onBlur, onFocus() if(document.forms[0].rad1[0].checked == true) { alert(“button is checked”) }

Bhawna Mallick 17 Checkbox object Properties  checked, defaultChecked, name, type, value Methods  click() Event Handler  onClick, onBlur, onFocus() if(document.form1.chk1.checked==true) { red=255; } else { red=0; }

Bhawna Mallick 18 Button, reset, submit Objects Properties  form, name, type, value Methods  click( ), blur( ), focus( ) Event Handler  onClick, onBlur, onFocus, onMouseDown, onMouseUp The disabled property ( Applicable for all the form controls)  If this attribute is set to true, the button is disabled  This attribute can use with all other form elements to disable the element document.forms[0].b1.disabled=true;  To enable a control, you must set the disabled property value as false.

Bhawna Mallick 19 Using submit() method When a user clicks on a submit button, the form is sent to the address specified in the action attribute. (used with server side programming) Submit button can invoke the action page without using any client side scripting. If you want submit a form other than using the submit button, you need to invoke the submit() method of the form. When a user clicks on a radio button, we can submit the page using the following code. function submitForm(){ document.form1.action=“ document.form1.submit(); } /* this is mainly required in the server side programming */

Bhawna Mallick 20 Select Object The user can choose one or more items from a selection list, depending on how the list was created. Properties Methods  blur(), focus() Event Handler  onBlur, onChange, onFocus lengthReflects the number of options in the selection list. optionsReflects the OPTION tags. selectedIndexReflects the index of the selected option (or the first Selected option, if multiple options are selected).

Bhawna Mallick 21 Option Object An option in a selection list. Properties indexThe zero-based index of an element in the Select.options array. selectedSpecifies the current selection state of the option textSpecifies the text for the option valueSpecifies the value for the option lengthThe number of elements in the Select.options array.

Bhawna Mallick 22 Form Validation Client-side form validation is an important part of a website where data needs to be collected from the user. What to Validate?  required fields are entered  correct data type is entered  user's input conforms to a certain standard (such as telephone numbers, , etc.).  Data is in a certain range. (age between 1 and 100)

Bhawna Mallick 23 Advantages of Client Side Validation Client side validation is faster than server side validation Validating forms on the client side lightens the load on the server and allows for you to format data before it is sent for processing. Using server-side validation, there is noticeable lag time when data the user submits contains an error. Form data must travel over the Internet and be examined by the server. Developers will find it easier and more intuitive to do form validation with JavaScript than with server side techniques since the code has already been written and is easy to implement.

Bhawna Mallick 24 Form Validation Let's look at an example. You have a form with the following fields:  Name  Age  What to validate  All the fields are entered.  Name should be a string with minimum 3 Character.  Age should be a number between 1 and 100  should be a string with an character. Make use of String functions and top level functions to validate the fields

Bhawna Mallick 25 Using alert() method for Debugging Everyone makes mistakes writing JavaScript programs. JavaScript provides error messages as a means to help you spot mistakes. Use JavaScript alert statement to display information about the values that your JavaScript is using. Processing of the JavaScript is halted until the OK button is clicked. Series of alert statements like this one can be usefully inserted into the code.  when a function is executed  how many times a loop is executed  which path in an if statement is followed

Thank You