JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu

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

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
The Web Warrior Guide to Web Design Technologies
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Chapter 6 © 2002 by Addison Wesley Longman, Inc. 1 Chapter 6 Sebesta: Programming the World Wide Web.
Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Questions? Assignment 1 due January 31 (TUESDAY) Discussions on JAVASCRIPT.
JavaScript, Third Edition
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
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.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Scripting Languages.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Chapter 4 © 2003 by Addison-Wesley, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape.
CP476 Internet Computing JavaScript Client-Side Programming 1 1. What is JavaScript –Another script language for both client-side and sever-side programming.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Chapter 4 The Basics of JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Overview of JavaScript - Originally developed by Netscape,
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Chapter 4 © 2010 by Addison Wesley Longman, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
JavaScript - A Web Script Language Fred Durao
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
1 Introduction to Web Application Introduction to Java Script.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Chapter 4 © 2005 by Addison Wesley Longman, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 © 2014 by Pearson Education Overview of JavaScript - Originally developed by Netscape by Brendan Eich, as LiveScript - Became a joint venture.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 4 © 2013 by Pearson Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape and Sun.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
4.1 Overview of JavaScript
4.1 Overview of JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Chapter 4 Client-Side Programming: the JavaScript Language
Chapter 4 Basics of JavaScript.
Introduction to Scripting
JavaScript Syntax and Semantics
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript and Ajax (Expression and Operators)
Chapter 8 JavaScript: Control Statements, Part 2
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
The Basics of JavaScript
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript CS 4640 Programming Languages for Web Applications
4.1 Overview of JavaScript
JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript CS 4640 Programming Languages for Web Applications
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu

JS1-2 JS  Introduction  General Syntactic Characteristics  JS write to the XHTML document  Write vs Writeln  Typical JS dialogs  Primitives, Operations, & Expressions  Primitives: Number, String, Boolean, Undefined, Null  Numeric operators  String catenation  Coercions  Control Statements  Selection statements  Loop statements

JS1-3 Introduction  JavaScript scripting language  Enhance functionality and appearance  Client-side scripting Make pages more dynamic and interactive  Foundation for complex server-side scripting  Program development  Program control JavaScript Examples

JS1-4 Things you should know about JS  JavaScript can be used to replace some of what is typically done with applets (except graphics)  JavaScript can be used to replace some of what is done with CGI (but no file operations or networking)  User interactions through forms are easy  The Document Object Model (DOM) makes it possible to support dynamic HTML documents with JavaScript  Event-Driven Computation  User interactions with HTML documents in JavaScript use the event-driven model of computation  User interactions with form elements can be used to trigger execution of scripts

JS1-5 Things you should know about JS  JavaScript is NOT an object-oriented programming language  Does not support class-based inheritance Cannot support polymorphism  Has prototype-based inheritance, which is much different  JavaScript Objects  JavaScript objects are collections of properties, which are like the members of classes in Java and C++ Date.getTime()  JavaScript has primitives for simple types  All JavaScript objects are accessed through references

JS1-6 General Syntactic Characteristics  For this book, all JavaScript scripts will be embedded in HTML documents  Either directly, as in -- JavaScript script –  Or indirectly, as a file specified in the src attribute of, as in <script type = "text/javaScript" src = "myScript.js"> Example

JS1-7 General Syntactic Characteristics  Identifier form: begin with a letter or underscore, followed by any number of letters, underscores, and digits  Case sensitive  variable1 and Variable1 are different  25 reserved words, plus future reserved words  - Comments: both // and /* … */

JS1-8 General Syntactic Characteristics  Scripts are usually hidden from browsers that do not include JavaScript interpreters by putting them in special comments <!— -- JavaScript script – //-->  Semicolons can be a problem  They are “somewhat” optional  Problem: When the end of the line CAN be the end of a statement – JavaScript puts a semicolon there Return X;

JS1-9 JS  Introduction  General Syntactic Characteristics  JS write to the XHTML document  Write vs Writeln  Typical JS dialogs  Primitives, Operations, & Expressions  Primitives: Number, String, Boolean, Undefined, Null  Numeric operators  String catenation  Coercions  Control Statements  Selection statements  Loop statements

JS1-10 Simple Program: Printing a line of text in a web page A First Program in JavaScript <!-- document.writeln(" Welcome to JavaScript Programming! " ); // --> Example

JS1-11 Write vs Writeln  Document.writeln(“This is the end!”)  Document.write(“This is the end!\r\n”); Example2Example 3 Want to know what was written to the browser? Mozilla Script Tracer

JS1-12

JS1-13 Typical JS dialogs  “Window” object  JavaScript model for the browser window  Three methods: Prompt, confirm, alert Cause the browser to wait for a user response Example alert, confirm, prompt

JS1-14 Number  parseInt(variable);  parseFloat(variable); Example

JS1-15 JS  Introduction  General Syntactic Characteristics  JS write to the XHTML document  Write vs Writeln  Typical JS dialogs  Primitives, Operations, & Expressions  Primitives: Number, String, Boolean, Undefined, Null  Numeric operators  String catenation  Coercions  Control Statements  Selection statements  Loop statements

JS1-16 Primitives, Operations, & Expressions  Five primitive typesExampleExample  Number, String, Boolean, Undefined, or Null  JavaScript is dynamically typed – any variable can be used for anything (primitive value or reference to any object)  The interpreter determines the type of a particular occurrence of a variable complication  Variables can be either implicitly or explicitly declared var sum = 0, today = "Monday", flag = false;

JS1-17 Primitives, Operations, & Expressions  Number, String, and Boolean have wrapper objects (Number, String, and Boolean)  All numeric values are stored in double-precision floating point  String literals are delimited by either ' or “ Can include escape sequences (e.g., \t)  Boolean values are true and false  The only Null value is null  The only Undefined value is undefined  In the cases of Number and String, primitive values and objects are coerced back and forth so that primitive values can be treated essentially as if they were objects  Var num_v=Number(str_v);  Var str_v=String(num_v);  Var str_v=num_v.toString(); str_v=num_v.toString(2);

JS1-18 Primitives, Operations, & Expressions  Numeric operators - ++, --, +, -, *, /, %  All operations are in double precision A++ -  A=A+1; A++ vs ++A; Example Example  The Math Object  Provides floor, round, max, min, trig functions, etc. e.g., Math.cos(x) Example Example Math functions supported by JS mathfunc.htm

JS1-19 Primitives, Operations, & Expressions  The Number ObjectExampleExample  Some useful properties: MAX_VALUE, MIN_VALUE, NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY, PI –e.g., Number.MAX_VALUE An arithmetic operation that creates overflow returns NaN NaN is not == to any number, not even itself Test for it with isNaN(x) Number object has several methods –toStringtoFixedtoExponentialtoPrecision valueOf

JS1-20 Primitives, Operations, & Expressions  String catenation operator: + -ExampleExample  Coercions  Catenation coerces numbers to strings Numeric operators (other than +) coerce strings to numbers (if either operand of + is a string, it is assumed to be catenation Conversions from strings to numbers that do not work return NaN  Explicit conversions Use the String and Number constructors Use toString method of numbers Use parseInt and parseFloat on strings  String properties & methods  length e.g., var len = str1.length; (a property)  charAt(position) e.g., str.charAt(3)  indexOf(string) e.g., str.indexOf('B')  substring(from, to) e.g., str.substring(1, 3)  toLowerCase() e.g., str.toLowerCase()

JS1-21 Primitives, Operations, & Expressions  The typeof operatorExampleExample  Returns "number", "string", or "boolean" for primitives; returns "object" for objects and null  The Data Object  Create one with the Date constructor (no params)  Local time methods of Date: toLocaleString – returns a string of the date getDate – returns the day of the month getMonth – returns the month of the year (0 – 11) getDay – returns the day of the week (0 – 6) getFullYear – returns the year getTime – returns the number of milliseconds since January 1, 1970 getHours – returns the hour (0 – 23) getMinutes – returns the minutes (0 – 59) getMilliseconds – returns the millisecond (0 – 999) Example

JS1-22 JS  Introduction  General Syntactic Characteristics  JS write to the XHTML document  Write vs Writeln  Typical JS dialogs  Primitives, Operations, & Expressions  Primitives: Number, String, Boolean, Undefined, Null  Numeric operators  String catenation  Coercions  Control Statements  Selection statements  Loop statements

JS1-23 Control Statements  Compound statements are delimited by braces, but compound statements are not blocks  NO local variables  Control expressions – three kinds  Primitive valuesExampleExample If it is a string, it is true unless it is empty or "0" If it is a number, it is true unless it is zero  Relational ExpressionsExampleExample ==, !=,, = Operands are coerced if necessary(ASCII) –string vs number, it attempts to convert the string to a number –Boolean vs non-boolean, the Boolean operand is coerced to a number (1 or 0)  Compound Expressions && || ! Example Example

JS1-24 Selection Statements  The usual if-then-else (clauses can be either single statements or compound statements)  Switch switch (expression) { case value_1: // value_1 statements case value_2: // value_2 statements … [default: // default statements] }  The statements can be either statement sequences or compound statements  The control expression can be a number, a string, or a Boolean  Different cases can have values of different types ExampleMoreExampleMore Complex ExampleComplex Example

JS1-25 Loop Statements  for (init; control; increment) { statement or cmpnd ExampleContinue ExampleContinue }  while (control_expression) Example Example { statement or cmpnd }  do {ExampleExample statement or compound }while (control_expression)

JS1-26

JS1-27 JS  Introduction  General Syntactic Characteristics  JS write to the XHTML document  Write vs Writeln  Typical JS dialogs  Primitives, Operations, & Expressions  Primitives: Number, String, Boolean, Undefined, Null  Numeric operators  String catenation  Coercions  Control Statements  Selection statements  Loop statements