JavaScript Part 2 – Page 1 of 35CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: JavaScript Part 2.

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
The Web Warrior Guide to Web Design Technologies
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Information Technology Center Hany Abdelwahab Computer Specialist.
Computer Science 103 Chapter 4 Advanced JavaScript.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Introduction to scripting
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
1 CS101 Introduction to Computing Lecture 35 Mathematical Methods (Web Development Lecture 12)
CST JavaScript Validating Form Data with JavaScript.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
4.1 JavaScript Introduction
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
JavaScript Part 1.
JavaScript Informatics for economists I. Introduction Programming language used in web pages. Simple and easy to use – written in HTML document. Client.
JavaScript Lecture 6 Rachel A Ober
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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Chapter 4: Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators,
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.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
The Grammar of JavaScript.  Each line of a script is a statement  An instruction that JavaScript can evaluate (make sense of)  Ends with a semicolon;
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.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Expressions and Data Types Professor Robin Burke.
REEM ALMOTIRI Information Technology Department Majmaah University.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Math class Random() method Floor method Top-level parseInt function
Chapter 6 JavaScript: Introduction to Scripting
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Introduction to Scripting
14 A Brief Look at JavaScript and jQuery.
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

JavaScript Part 2 – Page 1 of 35CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: JavaScript Part 2

JavaScript Part 2 – Page 2 of 35CSCI 2910 – Client/Server-Side Programming Today’s Goals Today’s lecture will cover: –More objects, properties, and methods of the DOM –The Math object –Introduction to form validation

JavaScript Part 2 – Page 3 of 35CSCI 2910 – Client/Server-Side Programming Intermediate File vs. HTML Output A sometimes difficult concept is that the output of a JavaScript script is not output to the browser window, but instead is output to the “intermediate” HTML file that the browser will interpret for display. XHTML file containing scripts XHTML with script output replacing scripts Document displayed in browser window Browser processes scripts

JavaScript Part 2 – Page 4 of 35CSCI 2910 – Client/Server-Side Programming Intermediate File vs. HTML Output (continued) Since the JavaScript output is to be interpreted by a browser as HTML, the output must contain tags. Example – Assume we want a heading level 1 with a line break in the middle: –Wrong: document.write("This is my \n page title"); –Right: document.write(" This is my page title ");

JavaScript Part 2 – Page 5 of 35CSCI 2910 – Client/Server-Side Programming write vs. writeln There are two document object methods used to write –document.write(string) –document.writeln(string) The only difference between the two is that writeln appends a carriage return/linefeed (\n) to the end of the string when printing to the intermediate file.

JavaScript Part 2 – Page 6 of 35CSCI 2910 – Client/Server-Side Programming Prompting as Page Loads Remember that scripts within the body are executed as they are encountered You can take advantage of this by prompting the user for information as the page loads using a function such as window.prompt().

JavaScript Part 2 – Page 7 of 35CSCI 2910 – Client/Server-Side Programming Prompting as Page Loads (continued) <!-- var head_color; head_color = window.prompt("What color would you like to display these headings in? (Enter web color)"); document.writeln(" " + "My Title" + " "); //-->

JavaScript Part 2 – Page 8 of 35CSCI 2910 – Client/Server-Side Programming Double vs. Single Quotes As with any language that relies heavily on the use of output strings, we must have a way to identify quotation marks within a string without affecting the way the interpreter views the string. In JavaScript, there are three ways to embed quotation marks within a string: –use single quotes within a string identified using double quotes –use double quotes within a string identified using single quotes –use the JavaScript escape characters \' or \"

JavaScript Part 2 – Page 9 of 35CSCI 2910 – Client/Server-Side Programming Double vs. Single Quotes (cont.) Examples: document.write(" "); document.write(' '); document.write(" "); All three methods should work regardless of browser

JavaScript Part 2 – Page 10 of 35CSCI 2910 – Client/Server-Side Programming Declaring Variables Variables are declared using the keyword var Example: var int_value, string_value When variables are declared, they are not assigned a default value, unless specified by the programmer All variables in JavaScript can contain a value of any data type, i.e., JavaScript does not rigorously follow types an will try to convert between types null is a valid variable value

JavaScript Part 2 – Page 11 of 35CSCI 2910 – Client/Server-Side Programming Parsing Functions parseInt(string, radix) -- returns the first integer in the string. The radix argument specifies the base in which the number is represented in the string, e.g., 16 (hexadecimal), 10 (decimal), or 2 (binary). Example: parseInt("313 Gilbreath", 10); would return 313 If the first character is not a number, then the function returns "NaN" indicating the value is not a number.

JavaScript Part 2 – Page 12 of 35CSCI 2910 – Client/Server-Side Programming Parsing Functions (continued) parseFloat(string) – returns the first floating point number in the string. Example: parseFloat("2.98% of students"); would return 2.98 If the first character is not a number, then the function returns "NaN" indicating the value is not a number. This includes characters such as $ or #.

JavaScript Part 2 – Page 13 of 35CSCI 2910 – Client/Server-Side Programming isNaN() isNaN(value) – returns a true or false based on whether value represents a number or not. "value" can be a string containing a number. Helpful with validation of forms. Examples: –isNaN("David Tarnoff") would return true –isNaN(4*5) would return false –isNaN("315") would return false

JavaScript Part 2 – Page 14 of 35CSCI 2910 – Client/Server-Side Programming unescape() In some cases, strings are encountered that have certain characters replaced with escape characters. For example, a URL often replaces spaces with %20 and the symbol with %40. unescape(encodedstring) – goes through a string replacing escape characters with original characters.

JavaScript Part 2 – Page 15 of 35CSCI 2910 – Client/Server-Side Programming unescape() (continued) For example: document.write( unescape("My%20e- mail%20is%3A%20tarnoff%40etsu.edu%21")); would output as: My is:

JavaScript Part 2 – Page 16 of 35CSCI 2910 – Client/Server-Side Programming Math Object JavaScript provides this utility object for your use in scripting. The Math object isn’t part of the DOM, i.e., it is not a conceptual component of a web page. The Math object is a stand alone object provided for use with mathematical operations

JavaScript Part 2 – Page 17 of 35CSCI 2910 – Client/Server-Side Programming Math Object Properties Math.E – returns the base of natural logarithms, i.e., e ≈ Math.LN10 – returns the natural logarithm of 10, i.e., ln(10) ≈ Math.LN2 – returns the natural logarithm of 2, i.e., ln(2) ≈ Math.LOG10E – returns the base 10 logarithm of e, i.e., log 10 (e) ≈ Math.LOG2E – returns the base 2 logarithm of e, i.e., log 2 (e) ≈ Math.PI – returns the ratio of the circuference of a circle to its diameter, i.e., pi ≈ Math.SQRT1_2 – returns the value of 1 divided by the square root of 2, i.e., 1/(  2) ≈ Math.SQRT2 – returns the square root of 2, i.e.,  2 ≈

JavaScript Part 2 – Page 18 of 35CSCI 2910 – Client/Server-Side Programming Math Object Methods Math.abs(x) – returns the absolute value of x Math.acos(x) – returns the arccosine of x as a numeric value between 0 and PI radians Math.asin(x) – returns the arcsine of x as a numeric value between -PI/2 and PI/2 radians Math.atan(x) – returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians Math.atan2(y, x) – returns the arctangent of the quotient of its arguments Math.ceil(x) – returns the smallest integer greater than or equal to x Math.cos(x) – returns the cosine of x where x is in radians Math.exp(x) – returns the value of e x where e is Euler's constant

JavaScript Part 2 – Page 19 of 35CSCI 2910 – Client/Server-Side Programming Math Object Methods (continued) Math.floor(x) – returns the largest integer less than or equal to x Math.log(x) – returns the natural logarithm of x Math.max(x, y) – returns the greater of x and y Math.min(x, y) – returns the lesser of x and y Math.pow(x, y) – returns the value of x y Math.random() – returns a pseudo-random number between 0 and 1 Math.round(x) – rounds x to the nearest integer Math.sin(x) – returns the sine of x where x is in radians Math.sqrt(x) – returns the square root x Math.tan(x) – returns the tangent of x where x is in radians

JavaScript Part 2 – Page 20 of 35CSCI 2910 – Client/Server-Side Programming Accessing Data from Forms Before we get to the point where we're trying to access data, let's talk a little about the form object and its properties and methods One way to "point" to a specific form object is to access the document object forms array. document.forms[n] The most reliable way to reference a form object is to consistently identify everything with the name and id attributes. document.formname document.forms["formname"]

JavaScript Part 2 – Page 21 of 35CSCI 2910 – Client/Server-Side Programming Form Object Properties action – Returns the URL address to which the form's data will be submitted. length – Returns the number of elements in the form. method – Returns a string specifying data submission method, i.e., either 'get' or 'post'. target – Returns the target window where the form's response will appear.

JavaScript Part 2 – Page 22 of 35CSCI 2910 – Client/Server-Side Programming Form Object Methods reset( ) – Resets the form to its default values. (Same result as clicking the reset button.) submit( ) – Submits the form's data. (Same result as clicking the submit button.)

JavaScript Part 2 – Page 23 of 35CSCI 2910 – Client/Server-Side Programming Accessing Element Values One way to "point" to a specific element in a form is to access the element array under the form object. document.formname.elements[n] where n equals the position the element holds in the order that the elements were added to the form. Huh? The most reliable way to reference an element of a form is to consistently identify everything with the name and id attributes. document.formname.elementname document.forms["formname"].elementname

JavaScript Part 2 – Page 24 of 35CSCI 2910 – Client/Server-Side Programming Form Element Object Properties defaultValue – sets or returns a string representing the default value of the element. name – sets or returns the element's name or id attribute. type – returns the element's type property. value – sets or returns the element's value attribute. Works differently for different elements.

JavaScript Part 2 – Page 25 of 35CSCI 2910 – Client/Server-Side Programming Form Element Object Methods blur( ) – removes the focus from the specified element click( ) – simulates a mouse-click for some elements focus( ) – returns focus to the specified element

JavaScript Part 2 – Page 26 of 35CSCI 2910 – Client/Server-Side Programming The Use of value Checkbox First Second Third One Two Three Four

JavaScript Part 2 – Page 27 of 35CSCI 2910 – Client/Server-Side Programming The Use of value (continued)

JavaScript Part 2 – Page 28 of 35CSCI 2910 – Client/Server-Side Programming The Use of value (continued) var output_string; function printVals() { output_string="Checkbox value = " + document.userinput.gen_check.checked + "\n" + "Radio button value = " + document.userinput.gen_radiobutton.value + "\n" + "Text value = " + document.userinput.gen_text.value + "\n" + "Select value = " + document.userinput.gen_select.value + "\n" + "File selection value = " + document.userinput.gen_file.value + "\n" + "Button value = " + document.userinput.gen_button.value + "\n"; document.userinput.output.value=output_string; }

JavaScript Part 2 – Page 29 of 35CSCI 2910 – Client/Server-Side Programming The Use of value (continued)

JavaScript Part 2 – Page 30 of 35CSCI 2910 – Client/Server-Side Programming The Use of value (continued) Some of the values make sense, e.g., the text value equaled the text in the box. Some of the values did not make sense –Checkbox value always equals "on" –Radio buttons always equal "undefined" –Button value equals the text on the button

JavaScript Part 2 – Page 31 of 35CSCI 2910 – Client/Server-Side Programming The Use of value (continued) Solutions –To read the checkbox values, use the property checked – returns "true" or "false" –Associate an onClick event for each radio button that modifies a variable –Associate an onClick event for the button to indicate when it is pressed.

JavaScript Part 2 – Page 32 of 35CSCI 2910 – Client/Server-Side Programming Form Validation A very common application of client-side scripts is for validating the data users have entered on a form. For example, we would like to make sure that the user has not done something like entered the word "dog" where the form asked for an age. The functions covered over the past two lectures will allow us to access form data and verify that it is correct.

JavaScript Part 2 – Page 33 of 35CSCI 2910 – Client/Server-Side Programming Simple Number Verification The form below creates a text box and a button. Enter an integer in this field:

JavaScript Part 2 – Page 34 of 35CSCI 2910 – Client/Server-Side Programming Simple Number Verification (continued) function integercheck() { if (isNaN(document.sample.justanumber)) { window.alert("This field requires an integer!"); document.sample.justanumber.focus(); }

JavaScript Part 2 – Page 35 of 35CSCI 2910 – Client/Server-Side Programming Checking for in function check( _string) { window.alert("Not a valid address!"); }