A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

Similar documents
CSC Web Programming. Introduction to JavaScript

Client-Side Web Technologies. JavaScript Part I

CGS 3066: Spring 2015 JavaScript Reference

3 The Building Blocks: Data Types, Literals, and Variables

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Computer Components. Software{ User Programs. Operating System. Hardware

3. Java - Language Constructs I

Program Fundamentals

JavaScript CS 4640 Programming Languages for Web Applications

Language Fundamentals Summary

CHAPTER 5: JavaScript Basics 99

An overview of Java, Data types and variables

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

CSC 1214: Object-Oriented Programming

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

JavaScript CS 4640 Programming Languages for Web Applications

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Introduction to JavaScript

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

Chapter 2: Using Data

JAVA Programming Fundamentals

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Introduction to C# Applications

Lecture 3. More About C

UNIT- 3 Introduction to C++

2 rd class Department of Programming. OOP with Java Programming

Accelerating Information Technology Innovation

COMP 202 Java in one week

Coding in JavaScript functions

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

JavaScript: The Basics

<form>. input elements. </form>

Computer Components. Software{ User Programs. Operating System. Hardware

Basics of Java Programming

Visual C# Instructor s Manual Table of Contents

HTML5 and CSS3 More JavaScript Page 1

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Chapter 2 Working with Data Types and Operators

Chapter 2: Using Data

Basic Types, Variables, Literals, Constants

Java Programming. Atul Prakash

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Declaration and Memory

C: How to Program. Week /Mar/05

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

The first sample. What is JavaScript?

Java Basic Datatypees

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Introduction to Programming Using Java (98-388)

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Chapter 2 - Introduction to C Programming

A First Program - Greeting.cpp

Java Programming Fundamentals. Visit for more.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

Java Notes. 10th ICSE. Saravanan Ganesh

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

What is PHP? [1] Figure 1 [1]

6.096 Introduction to C++ January (IAP) 2009

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Lexical Considerations

4. Inputting data or messages to a function is called passing data to the function.

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Chapter 2. Elementary Programming

Chapter 2 Elementary Programming

Chapter 2. Lexical Elements & Operators

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Java Overview An introduction to the Java Programming Language

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Programming Lecture 3

Exercise 1: Basic HTML and JavaScript

Lexical Considerations

2 nd Week Lecture Notes

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 2: Basic Elements of C++

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

Programming in C++ 4. The lexical basis of C++

The MaSH Programming Language At the Statements Level

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Java+- Language Reference Manual

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

1 Lexical Considerations

CSC 1107: Structured Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

Zheng-Liang Lu Java Programming 45 / 79

Index COPYRIGHTED MATERIAL

Variables and Typing

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Transcription:

Internet t Software Technologies JavaScript part one IMCNE A.A. 2008/09 Gabriele Cecchetti Why introduce JavaScript To add dynamicity and interactivity to HTML pages 2

What s a script It s a little interpreted program It s platform independent 3 What s JavaScript It is object oriented too. It is a scripting language for a browser It is simpler than Java It is loosely typed It is prototype based (inherited properties may vary) 4

JavaScript s goals To provide dynamicity, effectiveness and interactivity to HTML pages To develop application on client side; To check data input from user before submitting them to a server; To manage searches on client side; To store and load cookies. 5 How to include JavaScript Inside HTML document <script type= text/javascript > <!-- JavaScript code goes here //--> </script> External source file <script type= text/javascript src= myscript.js > 6

JavaScript Language version You can specify different version scripts to support different browser: <script type= text/javascript te t/ja asc pt language= JavaScript1.2 > asc Or you can reditect JavaScript not enabled browser to another page: <NOSCRIPT> <META HTTP-EQUIV REFRESH CONTENT="0; URL=anotherpage.html"> </NOSCRIPT> 7 Hello world example <html> <head> <title> Hello World</title> </head> <body> <script type= text/javascript > <-- document.write( Hello World! ) // --> </script> </body> </html> 8

When a script will be run? It s dependent by: code position (inside the document) code organization Generally it runs from: sequentially, inside <SCRIPT> element; loaded from external file; when an event handler is invoked; when JavaScript code is activated from HTML link. 9 Where to put the JavaScript? Scripts in the head section: Scripts to be executed when they are called, or when an event is triggered, go in the head section. When you place a script in the head section, you will ensure that the script is loaded before anyone uses it. Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page. You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section. 10

Example <html> <head> <title> onload event </title> <script language= JavaScript g > window.alert( Not in function ); // this is executed first function loading() { window.alert( onload event ); </script> </head> <body onload= loading() > <! call function loading() - -> </body> </html> 11 CONCEPTS & SYNTAX 12

JavaScript Statements A JavaScript statement is a command to the browser. The purpose of the command is to tell the browser what to do. JavaScript is a sequence of statements, which are executed in the sequence they are written. JavaScript Statements can be grouped together in blocks { JavaScript is case sensitive! 13 Comments // comment: one line /* comment (also several lines */ 14

Quotes Single and double quotes can be used: note the following example: alert( Don t do this! ) Error missing ) after argument list. alert( Don t do this! ) Correct with: alert( Don\ t do this! ) Or alert( Don t t do this! ) 15 Semicolon Semicolon ; is used as a instructions separator. New instructions can start each newline or after a semicolon. 16

Literals Literals are explicit quantities: Numbers: Decimal / Octal (0) / Hexadecimal (0x), Floating point and fixed point; Boolean (True/false); String arrays; Null value; Special characters. newline \n carriage return\ horiz. tab \t backspace \b linefeed \f backslash \\ single quote \ double quotes \" 17 Basic Types numeric 12, 113.256 string Ciao boolean true or false null null value undefined value non specified function function defined or built-inin 18

Variables Global variables Valid for the whole HTML document: They must declared> inside <SCRIPT> element, preferably in the head section, before they can be used, and not inside a function. Local variables Valid only inside the function where they are declared. They can be prefixed by var keyword. They can be initialized or leave to null value. They do not have any type. 19 Variables Example <html><head><title> Scoping Rules</title> <script language= JavaScript > <!-- var cc=0 ; // cc: global var. var dd=scr(); // dd: global var. document.writeln( Global variale: " + cc); document.writeln( writeln( Local variable: " + dd); function scr() { var cc =3; // cc now is a local variable return cc; // --> </script></head><body ></body></html> 20

Undefined variable If you declare a variable without assigning a value they are undefined; e.g. var mycolor alert(mycolor); return undefined. That is different to assign a null value var mycolor=null allocates the memory for that variable assigning the null value. 21 Variable names Only letters, number and underscore can be used as variable names The first char must always be a letter or underscore. Javascript is case-senditive Do not use JavaScript keywords. 22

JavaScript Keywords abstract do if package throw boolean double implements private throws break else import protected transient byte extends in public true case false instanceof return try catch final int short var char finally interface static void class float long super while const* for native switch with continue function new synchronized default goto null this 23 Loosed type variables JavaScript variables have not a specified type, so you can declare myvar= hello // or myvar=59 It s the embedded JavaScript compiler which detect the type. Different types of variables can be concatenated: another_var= my dog is + 7 The compiler will make the necessary conversions. 24

Common ways to convert variables types: From number to string: Using quotes (string concatenation); mystring=tostring(number); From string to number: parseint( 10 ) parsefloat( 10.5 ) eval - The eval() function evaluates a string and executes it as if it was script code; e.g. eval("x=10;y=20;document.write(x*y)"); 25 Operators Arithmetic operators Assignment operators Concatenation operators Comparison operators Logical operators Conditional operators 26

Arithmetic Operators + Addition - Subtraction * Multiplication / Division % Modulus (division remainder) ++ Increment -- Decrement 27 Assignment Operators = += x = x + y -= x = x - y *= x = x * y /= x = x / y %= x=x%y x 28

Concatenation operator Example document.writeln( abc + def ); produce the same effect of: document.writeln( abcdef ); or when you mix strings and numbers: document.writeln( writeln( x = +5); produce the same effect of: document.writeln( x = 5 ); 29 Comparison operator Operator Description Example == Is equal to X==8 is false === Is exacly equal x===5 is true, while x=== 5 is false!= Is not equal x!=8 is true > Is greater than x >8 is false < Is less than x >8 is true >= Is greater or equal than x>=8 is false <= Is less than or equal x<=8 is true than Example: if (age<18) document.write("too young"); 30

Logical operators Operator Description Example && and (x < 10 && y > 1) is true or (x==5 y==5) is false! Not!(x==y) is true 31 Conditional operator Syntax variable_name=(condition)?value1:value2 Example greeting=(visitor=="pres")?"dear President ":"Dear "; 32

Other operators operand1, operand2 typeof: typeof operand; void: void expression; or void (expression) 33 Flow control statements If Else Switch For Loop and While Loop Break Loops 34

If and If else statements if (condition) { code to be executed if condition is true if (condition) { code to be executed if condition is true else { code to be executed if condition is not true 35 Switch 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 36

For Loop and While Loop for (var=start; var<=end; var=var+incr) var+incr) { code to be executed var=start while (var<=endvalue) { code to be executed var=var+incr 37 Break and continue The break command will break the loop and continue executing the code that follows after the loop (if any). Example: for (i=0;i<=10;i++) { if (i==3) { break; document.write("the number is " + i + "<br />"); The continue command will break the current loop and continue with the next value. Example: for (i=0;i<=10;i++) i< i++) { if (i==3) { continue; document.write("the number is " + i + "<br />"); 38

Functions A function contains code that will be executed by an event or by a call to that function. You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external.js file). Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the <head> section. 39 How to define a Function function functionname(var1,var2,...,varx) { some code return somevalue // optional A function with no parameters must include the parentheses () after the function name. 40

Function Example (1/2) <html><head><title> Variable and functions </title> <script language= JavaScript > <!-- function test( s ) { var a=10; var c= 10 * s; document.writeln(c); // --> </script></head> <body onload= test(10)"> </body> </html> 41 Another Function Example (2/2) <html><head><title> Function Call</title><script language="javascript"> <!-- document.writeln(concat("1","2","3","4")); document.writeln(concat("1","2")); "2")); document.writeln(concat("oggi ","e\'"," Lunedi\'")); function concat() { var fin=" "; if (arguments.length <3) return Few Argoments!<br>"; for(var t=0; t<arguments.length; t++) fin += arguments[t]; fin+="<br>"; return fin; // --> </script></head><body ></body></html> 42

Built-in Function eval(string) eval then execute it isfinite(number) true, NaN isnan(value) true, false parsefloat(string) float, NaN parseint(string ti [, radix]) integer, NaNN Number(obj_ref) number String(obj_ref) string escape(string) string to RFC1738 unescape(string) RFC1738 to string 43