Client-Side programming with JavaScript 3

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
HTML Forms JavaScript Introduction / Overview Lab Homework #1 CS 183 4/8/2010.
JavaScript Forms Form Validation Cookies CGI Programs.
XP Tutorial 9 New Perspectives on JavaScript, Comprehensive1 Working with Cookies Managing Data in a Web Site Using JavaScript Cookies.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
COMP 519: Web Programming Autumn 2014 Event-driven programs and HTML form elements  event-driven programs  onload, onunload  HTML forms & attributes.
CSC4370/6370: Web Programming Event-driven programs and HTML form elements  event-driven programs  onload, onunload  HTML forms & attributes  button,
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.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
JavaScript Form Validation
INTRODUCTION TO WEB DEVELOPMENT AND HTML Lecture 07: Forms - Spring 2011.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
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.
Tutorial 14 Working with Forms and Regular Expressions.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
JavaScript, Fourth Edition
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
JavaScript Part 1.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Chapter 8 Cookies And Security JavaScript, Third Edition.
CIS 375—Web App Dev II JavaScript II.
Using Client-Side Scripts to Enhance Web Applications 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
LOGO FORMs in HTML CHAPTER 5 Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side.
ITCS373: Internet Technology Lecture 5: More HTML.
1 Website Development HTML Forms and JavaScript Event-driven programs and HTML form elements  event-driven programs  ONLOAD, ONUNLOAD  HTML forms &
Client side scripting - javascript Pertemuan 4 Matakuliah: Web Programming Tahun: 2009.
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Introduction to Client-Side Web Development Introduction to Client-Side programming using JavaScript JavaScript; application examples 10 th February 2005.
1 JavaScript
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.
Client-Side programming with JavaScript 2. Event-driven programs and HTML form elements event-driven programs  onload, onunload  HTML forms & attributes.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CSC 551: Web Programming Fall 2001 HTML forms & JavaScript events  HTML forms & attributes  button, text box, text area  selection list, radio button,
SEN 910 CSS/HTML Programming HTML Forms and JavaScript Event-driven programs and HTML form elements  event-driven programs  ONLOAD, ONUNLOAD  HTML forms.
HTML Forms. A form is simply an area that can contain form fields. Form fields are objects that allow the visitor to enter information - for example text.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
1 CSC 551: Web Programming Spring 2004 Event-driven programs and HTML form elements  event-driven programs  ONLOAD, ONUNLOAD  HTML forms & attributes.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Chapter 5: Intro to Scripting CIS 275—Web Application Development for Business I.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
INTERNET APPLICATIONS CPIT405 Forms, Internal links, meta tags, search engine friendly websites.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
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,
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Web Programming– UFCFB Lecture 17
Validation and Building Small Apps
Presentation transcript:

Client-Side programming with JavaScript 3

The JavaScript Switch Statement Syntax switch(n) { case 1: execute code block 1 break case 2: execute code block 2 Break default: code to be executed if n is different from case 1 and 2 }

The JavaScript Switch Statement <script type="text/javascript"> //You will receive a different greeting based //on what day it is. Note that Sunday=0, //Monday=1, Tuesday=2, etc. var d=new Date() theDay=d.getDay() switch (theDay) { case 5: document.write("Finally Friday") break case 6: document.write("Super Saturday") case 0: document.write("Sleepy Sunday") default: document.write("I'm looking forward to this weekend!") } </script> view page

The while loop <html> <body> <script type="text/javascript"> var i=0 while (i<=10) { document.write("The number is " + i) document.write("<br />")i=i+1 } </script> </body> </html> The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 The number is 10

The do...while Loop <html> <body> <script type="text/javascript"> var i=0 do { document.write("The number is " + i) document.write("<br />")i=i+1 } while (i<0) </script> </body> </html> The number is 0

JavaScript Break and Continue The break command will break the loop and continue executing the code that follows after the loop (if any). <html> <body> <script type="text/javascript"> var i=0 for (i=0;i<=10;i++) { if (i==3){break} document.write("The number is " + i) document.write("<br />") } </script> </body> </html> The number is 0 The number is 1 The number is 2

JavaScript Break and Continue The continue command will break the current loop and continue with the next value. <html> <body> <script type="text/javascript"> var i=0 for (i=0;i<=10;i++) { if (i==3){continue} document.write("The number is " + i) document.write("<br />") } </script> </body> </html> The number is 0 The number is 1 The number is 2 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 The number is 10

Submission Button //When user clicks on the “Submit” button, //the content of the form is sent to //another file. <form name="input" action="html_form_action.php" method="get"> Username: <input type="text" name="user"> <br> <input type="submit" value="Submit"> </form> Another type of input field is the SUBMIT type, this tells the browser to draw a button. When the user clicks on the button the browser knows to submit the contents of the form to the URL specified as the ACTION in the form tag. Submit inputs support the attribute VALUE which is the string you want displayed in the button. If you don't include a VALUE attribute the browser will put the string "Submit" in the button. Note that the NAME attribute is not required for a submit input. Output Username: view page

Submission Button Another type of input field is the SUBMIT type, this tells the browser to draw a button. When the user clicks on the button the browser knows to submit the contents of the form to the URL specified as the ACTION in the form tag. Submit inputs support the attribute VALUE which is the string you want displayed in the button. If you don't include a VALUE attribute the browser will put the string "Submit" in the button.

Submission Button Notes on the "get" method: This method appends the form-data to the URL in name/value pairs This method is useful for form submissions where a user want to bookmark the result There is a limit to how much data you can place in a URL (varies between browsers), therefore, you cannot be sure that all of the form-data will be correctly transferred Never use the "get" method to pass sensitive information! (password or other sensitive information will be visible in the browser's address bar) Notes on the "post" method: This method sends the form-data as an HTTP post transaction Form submissions with the "post" method cannot be bookmarked The "post" method is more robust and secure than "get", and "post" does not have size limitations

OnSubmit The OnSubmit event is used to validate ALL form fields before submitting it. <form method="post" action=“MMM.htm“ onsubmit="return checkForm()"> The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled

JavaScript indexOf() Method Definition and Usage The indexOf() method returns the position of the first occurrence of a specified string value in a string. Syntax stringObject.indexOf(searchvalue,fromindex) The indexOf() method is case sensitive! This method returns -1 if the string value to search for never occurs.

JavaScript Form Validation JavaScript can be used to validate input data in HTML forms before sending off the content to a server. Form data that typically are checked by a JavaScript could be: has the user left required fields empty? has the user entered a valid e-mail address? has the user entered a valid date? has the user entered text in a numeric field?

JavaScript Form Validation Required Fields: function validate_required(field,alerttxt) { with (field) if (value==null || value=="") {alert(alerttxt) return false} else {return true} }

JavaScript Form Validation function validate_form(thisform) { with (thisform) if (validate_required(email,"Email must be filled out!")==false) email.focus() return false } view page

JavaScript Form Validation Email format: function validate_email(field,alerttxt) { with (field) {apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) alert(alerttxt) return false } else {return true} view page

JavaScript Animation <a href="http://www.Google.com" target="_blank“ onmouseOver="mouseOver()“ onmouseOut="mouseOut()"> <img border="0" alt="Visit Google!"src="b_pink.gif" name="b1" /> </a>

JavaScript Animation <script type="text/javascript"> function mouseOver() { document.b1.src ="b_blue.gif“ } function mouseOut() document.b1.src ="planets.gif “ </script> view page

Cookies & JavaScript Recall that cookies are data files stored on the client machine can be accessed and/or modified by the server can also be accessed and/or modified directly by JavaScript code in a page (With JavaScript, you can both create and retrieve cookie values) Potential applications: e-commerce: remember customer name, past visits/purchases, password, … tutorials: remember past experience, performance on quizzes, … games: remember high score, best times, …

Cookies & JavaScript Examples of cookies: Name cookie - The first time a visitor arrives to your web page, he or she must fill in her/his name. The name is then stored in a cookie. Password cookie - The first time a visitor arrives to your web page, he or she must fill in a password. The password is then stored in a cookie. Date cookie - The first time a visitor arrives to your web page, the current date is stored in a cookie.

Cookies & JavaScript each Web page can have its own cookie Document.cookie is a string of attribute=value pairs, separated by ; "userName=Dave; score=100 ;expires=Mon, 21-Feb-09 00:00:01 GMT"

Escape/unscape function This function explains how to "escape" any text, HTML, or Javascript to make it generally unreadable to the common user. URL Escape Codes are two-character Hexadecimal (8-bit) values preceeded by a % sign. This is used primarily in browser URLs or for use when making cookies for characters .

cookie.js function setCookie(Attribute, Value) // Assumes: Attribute is a string // Results: stores Value under the name Attribute, expires in 30 days { var ExpireDate = new Date(); ExpireDate.setTime(ExpireDate.getTime() + (30*24*3600*1000)); document.cookie = Attribute + "=" + escape(Value) + "; expires=" + ExpireDate.toGMTString(); document.cookie= Attribute + "=" +escape(Value)+ ((expiredays==null) ? "" : ";expires="+ExpireDate) } function delCookie(Attribute) // Results: removes the cookie value under the name Attribute var now = new Date(); document.cookie = Attribute + "=; expires=" + now;

cookie.js function getCookie(Attribute) // Assumes: Attribute is a string // Results: gets the value stored under the Attribute { if document.cookie.indexOf(Attribute+"=")==-1 ) { return ""; } else { var begin = document.cookie.indexOf(Attribute+"=") + Attribute.length+1; var end = document.cookie.indexOf(";", begin); if (end == -1) end = document.cookie.length; Return unescape(document.cookie.substring(begin, end));

Cookie Example <html> <head> <title> Fun with Cookies </title> <script type="text/javascript" src= cookie.js"> </script> <script type="text/javascript"> function Greeting() // Results: displays greeting using cookie { visitCount = getCookie("visits"); if (visitCount == "") { alert("Welcome to my page, new friend."); setCookie("visits", 1); } else { visitCount = parseFloat(visitCount)+1; alert("Welcome back for visit #" + visitCount); setCookie("visits", visitCount); </head>

Cookie Example <body onload="Greeting();"> Here is the stuff in my page. <form name="ClearForm"> <div style="text-align: center;"> <input type="button" value="Clear Cookie“ onclick="delCookie('visits');"> </div> </form> </body> </html> view page

JavaScript Create Your Own Objects Propertie The syntax for accessing a property of an object is: objName.propName Methods You can call a method with the following syntax: objName.methodName() personObj.firstname="John“ personObj.lastname="Doe“ personObj.age=30 personObj.eyecolor="blue“ document.write(personObj.firstname) personObj.sleep()

JavaScript Create Your Own Objects The template defines the structure of an object: function person(firstname,lastname,age,eyecolor) { this.firstname=firstname this.lastname=lastname this.age=age this.eyecolor=eyecolor } myFather=new person("John","Doe",50,"blue") myMother=new person("Sally","Rally",48,"green")

JavaScript Create Your Own Objects function person(firstname,lastname,age,eyecolor) { this.firstname=firstname this.lastname=lastname this.age=age this.eyecolor=eyecolor this.newlastname=newlastname } function newlastname(new_lastname) {this.lastname=new_lastname} mySister.newlastname(“Mazen"). view page