About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript” –The name was changed when Java became.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

JavaScript Language Fundamentals. About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript”
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
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.
18-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Tutorial 10 Programming with JavaScript
24-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
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.
JavaScript, Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
12-Aug-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Week 9 PHP Cookies and Session Introduction to JavaScript.
2440: 211 Interactive Web Programming Expressions & Operators.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to JavaScript Gordon Tian
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
27-Oct-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
15-Nov-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
JavaScript, Fourth Edition
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
JavaScript is an object-based scripting language that is lightweight and cross-platform. 3-Feb-16 JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Overview JavaScript. Overview 2 About Basics Objects DOM.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
6-Jun-16 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
JavaScript and Ajax Week 10 Web site:
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
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.
Tutorial 10 Programming with JavaScript
PHP.
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
Tutorial 10 Programming with JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
The <script> Tag
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript” –The name was changed when Java became popular Statements in JavaScript resemble statements in Java, because both languages borrowed heavily from the C language –JavaScript should be fairly easy for Java programmers to learn –However, JavaScript is a complete, full-featured, complex language JavaScript is seldom used to write complete “programs” –Instead, small bits of JavaScript are used to add functionality to HTML pages –JavaScript is often used in conjunction with HTML “forms” JavaScript is reasonably platform-independent

Using JavaScript in a browser JavaScript code is included within tags: – document.write(" Hello World! ") ; Notes: –This simple code does the same thing as just putting Hello World! in the same place in the HTML document –The semicolon at the end of the JavaScript statement is optional You need semicolons if you put two or more statements on the same line It’s probably a good idea to keep using semicolons

Web browser HTML Page: …code..… Desktop access Internet HTML/HTTP TCP/IP HTML/HTTP TCP/IP Web (HTTP) Server HTML pages w/ embedded script Remote host built-in JavaScript interpreter Web Architecture for JavaScript "CLIENT""SERVER"

Where to put JavaScript JavaScript can be put in the or in the of an HTML document –JavaScript functions should be defined in the This ensures that the function is loaded before it is needed JavaScript can be put in a separate.js file – –Put this HTML wherever you would put the actual JavaScript code –An external.js file lets you use the same JavaScript on multiple HTML pages –The external.js file cannot itself contain a tag JavaScript can be put in HTML form object, such as a button –This JavaScript will be executed when the form object is used

Primitive data types JavaScript has Five “primitive” types: number, string, and boolean, undefined, null Numbers includes both integer and floating-point values. Strings may be enclosed in single quotes or double quotes –Strings can contains \n (newline), \" (double quote), etc. Booleans are either true or false

Variables Variables are declared with a var statement: –var pi = , x, y, name = "Dr. Dave" ; –Variables names must begin with a letter or underscore – Variable names are case-sensitive –Variables are untyped (they can hold values of any type) –The word var is optional (but it’s good style to use it) Variables declared within a function are local to that function (accessible only within that function) Variables declared outside a function are global (accessible from anywhere on the page)

Operators, I Because most JavaScript syntax is borrowed from C (and is therefore just like Java), we won’t spend much time on it Arithmetic operators: + - * / % Comparison operators: = > Logical operators: && || ! ( && and || are short-circuit operators) Bitwise operators: & | ^ ~ > >>> Assignment operators: += -= *= /= %= >= >>>= &= ^= |=

Operators, II String operator: + The conditional operator: condition ? value_if_true : value_if_false Special equality tests: –== and != try to convert their operands to the same type before performing the test –=== and !== consider their operands unequal if they are of different types

Comments Comments are as in C or Java: –Between // and the end of the line –Between /* and */ Java’s javadoc comments, /**... */, are treated just the same as /*... */ comments; they have no special meaning in JavaScript

Statements, I Most JavaScript statements are also borrowed from C –Assignment: greeting = "Hello, " + name; –Compound statement: { statement ;...; statement } –If statements: if ( condition ) statement ; if ( condition) statement ; else statement ; –Familiar loop statements: while ( condition ) statement ; do statement while ( condition ); for ( initialization ; condition ; increment ) statement ;

An Example Code for “for” statement for Loop Example for(i=0, j=0 ; i<10 ; i++, j+=2) { document.write(i + " + " + j + " = " + (i+j) + " "); }

Statements, II The switch statement: switch ( expression) { case label : statement ; break; case label : statement ; break;... default : statement ; } Other familiar statements: –break; –continue;

Object literals You don’t declare the types of variables in JavaScript JavaScript has object literals, written with this syntax: –{ name1 : value1,..., nameN : valueN } Example (from Netscape’s documentation): –car = {myCar: "Saturn", 7: "Mazda", getCar: CarTypes("Honda"), special: Sales} The fields are myCar, getCar, 7 (this is a legal field name), and special "Saturn" and "Mazda" are Strings CarTypes is a function call Sales is a variable you defined earlier –Example use: document.write("I own a " + car.myCar);

Three ways to create an object You can use an object literal: –var course = { number: "CIT597", teacher="Dr. Dave" } You can use new to create a “blank” object, and add fields to it later: –var course = new Object(); course.number = "CIT597"; course.teacher = "Dr. Dave"; You can write and use a constructor: –function Course(n, t) { // best placed in this.number = n; this.teacher = t; } –var course = new Course("CIT597", "Dr. Dave");

Array literals You don’t declare the types of variables in JavaScript JavaScript has array literals, written with brackets and commas –Example: color = ["red", "yellow", "green", "blue"]; –Arrays are zero-based: color[0] is "red" If you put two commas in a row, the array has an “empty” element in that location –Example: color = ["red",,, "green", "blue"]; color has 5 elements –However, a single comma at the end is ignored Example: color = ["red",,, "green", "blue”,]; still has 5 elements

Four ways to create an array You can use an array literal: var colors = ["red", "green", "blue"]; You can use new Array() to create an empty array: –var colors = new Array(); –You can add elements to the array later: colors[0] = "red"; colors[2] = "blue"; colors[1]="green"; You can use new Array( n ) with a single numeric argument to create an array of that size –var colors = new Array(3); You can use new Array( …) with two or more arguments to create an array containing those values: –var colors = new Array("red","green", "blue");

The length of an array If myArray is an array, its length is given by myArray.length Array length can be changed by assignment beyond the current length –Example: var myArray = new Array(5); myArray[10] = 3; Arrays are sparse, that is, space is only allocated for elements that have been assigned a value –Example: myArray[50000] = 3; is perfectly OK –But indices must be between 0 and As in C and Java, there are no two-dimensional arrays; but you can have an array of arrays: myArray[5][3]

Arrays and objects Arrays are objects car = { myCar: "Saturn", 7: "Mazda" } –car[7] is the same as car.7 –car.myCar is the same as car["myCar"] If you know the name of a property, you can use dot notation: car.myCar If you don’t know the name of a property, but you have it in a variable (or can compute it), you must use array notation: car.["my" + "Car"]

Array functions If myArray is an array, –myArray.sort() sorts the array alphabetically –myArray.sort(function(a, b) { return a - b; }) sorts numerically –myArray.reverse() reverses the array elements –myArray.push(…) adds any number of new elements to the end of the array, and increases the array’s length –myArray.pop() removes and returns the last element of the array, and decrements the array’s length –myArray.toString() returns a string containing the values of the array elements, separated by commas

The for…in statement You can loop through all the properties of an object with for ( variable in object ) statement ; –Example: for (var prop in course) { document.write(prop + ": " + course[prop]); } –Possible output: teacher: Dr. Dave number: CIT597 –The properties are accessed in an undefined order –If you add or delete properties of the object within the loop, it is undefined whether the loop will visit those properties –Arrays are objects; applied to an array, for…in will visit the “properties” 0, 1, 2, … –Notice that course["teacher"] is equivalent to course.teacher You must use brackets if the property name is in a variable

The with statement with ( object ) statement ; uses the object as the default prefix for variables in the statement For example, the following are equivalent: –with (document.myForm) { result.value = compute(myInput.value) ; } –document.myForm.result.value = compute(document.myForm.myInput.value);

Functions Functions should be defined in the of an HTML page, to ensure that they are loaded first The syntax for defining a function is: function name ( arg1, …, argN ) { statements } –The function may contain return value ; statements –Any variables declared within the function are local to it The syntax for calling a function is just name ( arg1, …, argN ) Simple parameters are passed by value, objects are passed by reference

Warnings JavaScript is a big, complex language –We have only scratched the surface –It’s easy to get started in JavaScript, but if you need to use it heavily, plan to invest time in learning it well –Write and test your programs a little bit at a time JavaScript is not totally platform independent –Expect different browsers to behave differently –Write and test your programs a little bit at a time Browsers aren’t designed to report errors –Don’t expect to get any helpful error messages –Write and test your programs a little bit at a time