javascript – introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05js.pdf ·...

31
JavaScript – Introduction Web Technologies I. Zsolt Tóth University of Miskolc 2018 Zsolt Tóth (UM) JavaScript – Introduction 2018 1 / 31

Upload: others

Post on 03-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

JavaScript – IntroductionWeb Technologies I.

Zsolt Tóth

University of Miskolc

2018

Zsolt Tóth (UM) JavaScript – Introduction 2018 1 / 31

Page 2: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Introduction

Table of Contents

1 Introduction

2 SyntaxVariablesControl Flow StatementsFunctions

3 {B,D}OMDocument Object ModelBrowser Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 2 / 31

Page 3: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Introduction

JavaScript

untypedhigh levelObject basedNot object orientedScript languageClient sideBrowser interprets

+ Simple+ Easy-to-learn+ Form checking / validation+ Animated elements+ AJAX+ JSON Processing- Security

Zsolt Tóth (UM) JavaScript – Introduction 2018 3 / 31

Page 4: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Introduction

History

1995 May Netscape Mocha1995 SeptMocha→ LiveScript1995 DecLiveScript → JavaScript1998 StandardizationISO Standard

JQueryJavaScript Object NotationTypeScriptAngularJS

Zsolt Tóth (UM) JavaScript – Introduction 2018 4 / 31

Page 5: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Introduction

Usage

HTML document<script> tagWithin HTML

<head><body>

External file*.jslinking

function definitionsC, Java like syntax

< s c r i p t >a l e r t ("My F i r s t JavaScr ip t ") ;</ s c r i p t >

< s c r i p t s rc =" myScr ipt . j s " > </ s c r i p t >

Zsolt Tóth (UM) JavaScript – Introduction 2018 5 / 31

Page 6: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Introduction

Events

onloadonsubmitonchangeonkeydownonkeyuponclickondbclick

<bodyonload =" a l e r t (’ He l lo World ! ’ ) " >. . .</body>

Zsolt Tóth (UM) JavaScript – Introduction 2018 6 / 31

Page 7: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

Table of Contents

1 Introduction

2 SyntaxVariablesControl Flow StatementsFunctions

3 {B,D}OMDocument Object ModelBrowser Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 7 / 31

Page 8: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

Definition

untypedvar

Starts with character, $, _

Case-sensitiveundefined

null

j s > x = ’ 5 ’ ;5js > y = " 5 " ;5js > y = x + y ;55js >

Zsolt Tóth (UM) JavaScript – Introduction 2018 8 / 31

Page 9: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

String, Number, Boolean

String"’Captain’ Jack Sparrow"’"Captain" Jack Sparrow’

Number42, 42.003.1415e3 = 3141.53.1415e-1 = 0.31415

Booleantruefalse

x =1;1js > y = t rue ;t r uejs > x + y2x −y0js >

Zsolt Tóth (UM) JavaScript – Introduction 2018 9 / 31

Page 10: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

Arrays

index between 0 and n-1array[index ]Contains

Simple typesArraysObjectsFunctions

j s > var cars =[ " t r aban t " , "bmw " ] ;j s > carst rabant ,bmwjs > cars [ 5 ] = " panda "pandajs > carst rabant ,bmw, , , , pandajs >

Zsolt Tóth (UM) JavaScript – Introduction 2018 10 / 31

Page 11: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

Obejcts

data structure. operatorVariables (fields)Methods

var person=new Object ( ) ;person . f i r s tname ="John " ;person . lastname ="Doe " ;person . age=50;person . eyecolor =" blue " ;

Zsolt Tóth (UM) JavaScript – Introduction 2018 11 / 31

Page 12: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

API Objects

MathBasic mathematical functionsand constantsround()

random() ∈ [0,1]sqrt()

max()

min()

PI

E

Datenew Date();

Current datems1970-01-01

var d = new Date ( ) ;a l e r t ( d + 7 ) ;

Zsolt Tóth (UM) JavaScript – Introduction 2018 12 / 31

Page 13: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Variables

API Objects

Stringstr[3]

"alma".length = 4Escape sequencesindexOf()

match()

replace()

toUpperCase()

toLowerCase()

split()

trim()

RegExpPattern recognition/pattern/modifier

new RegExp(pattern,modifier);

Examples[abc], [∧abc], [A-Z], [0-9],(a|b|c),.,\s*,+,? {n}, {n,m}

Modifiersi case–insensitive

g global searchJavaScript

m multilinetest()

Zsolt Tóth (UM) JavaScript – Introduction 2018 13 / 31

Page 14: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Control Flow Statements

Table of Contents

1 Introduction

2 SyntaxVariablesControl Flow StatementsFunctions

3 {B,D}OMDocument Object ModelBrowser Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 14 / 31

Page 15: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Control Flow Statements

if, switch

if (condition) {...}

if (condition) {...}else {...}

else ifblockcondition ? true :

false;

break!NumberChar

swi tch ( n ){case 0: . . . break ;case 1: . . . break ;d e f a u l t : . . .}

Zsolt Tóth (UM) JavaScript – Introduction 2018 15 / 31

Page 16: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Control Flow Statements

Conditions

== Value=== Value and type

!= Not equals!== Not equals or

different type<,<=,>,>= less, more, etc.

&& and|| or! negation

Zsolt Tóth (UM) JavaScript – Introduction 2018 16 / 31

Page 17: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Control Flow Statements

for

IterativeParameters

1 Initialization2 Condition3 Incrementation

f o r ( i = 0 ; i < 5 ; i ++){. . .}

ArraysObject properties

f o r ( var x i n ob j ){. . .}

Zsolt Tóth (UM) JavaScript – Introduction 2018 17 / 31

Page 18: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Control Flow Statements

while, do. . .while

whi le ( c o n d i t i o n ){. . .}

do{. . .}wh i le ( co n d i t i o n ) ;

Zsolt Tóth (UM) JavaScript – Introduction 2018 18 / 31

Page 19: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Control Flow Statements

break, continue

break

loopsswitchlabel

goto label!

continue

loopsgoto end of the looploop does not break!

Zsolt Tóth (UM) JavaScript – Introduction 2018 19 / 31

Page 20: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Functions

Table of Contents

1 Introduction

2 SyntaxVariablesControl Flow StatementsFunctions

3 {B,D}OMDocument Object ModelBrowser Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 20 / 31

Page 21: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Functions

Function Definition

Untyped¿ parameters ?¿ return type ?¿ return ?

f u n c t i o n sayHel lo ( ) {a l e r t ( ’ He l lo World ! ’ ) ;

}

Zsolt Tóth (UM) JavaScript – Introduction 2018 21 / 31

Page 22: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Functions

Parameters

enumerate variables ,Type is not definedVariable names

a, b, c Xname, job, grossSalary X

f u n c t i o n add ( a , b ) {a l e r t ( a + b ) ;

}

Zsolt Tóth (UM) JavaScript – Introduction 2018 22 / 31

Page 23: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Functions

Return Type

Untyped!Function can return with

Simple typeArrayObjectFunction

Choose function nameswisely.

f u n c t i o n f a c t ( n ){i f ( n <= 1){r e t u r n 1 ;}r e t u r n n ∗ f a c t ( n−1);}

Zsolt Tóth (UM) JavaScript – Introduction 2018 23 / 31

Page 24: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Functions

Closure

Programming techniqueAnonymous functionRecord stores a functionFunction as a variableEvent handling

var add = ( f u n c t i o n ( ) {var counter = 0 ;r e t u r n f u n c t i o n ( ) {

r e t u r n counter += 1;}

} ) ( ) ;

Zsolt Tóth (UM) JavaScript – Introduction 2018 24 / 31

Page 25: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

Syntax Functions

Callback Functions

Function as a parameterExpected behaviorGeneralizationEvent handling

$ ( document ) . ready (f u n c t i o n ( ) {a l e r t ( ’document i s ready ! ’ ) ;} ) ;

Zsolt Tóth (UM) JavaScript – Introduction 2018 25 / 31

Page 26: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

{B,D}OM Document Object Model

Tartalomjegyzék

1 Introduction

2 SyntaxVariablesControl Flow StatementsFunctions

3 {B,D}OMDocument Object ModelBrowser Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 26 / 31

Page 27: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

{B,D}OM Document Object Model

Hierarchical Data Model

PCRContaining relationshipTree-structureDescribes HTML documentsNodes are objectsAPI

Zsolt Tóth (UM) JavaScript – Introduction 2018 27 / 31

Page 28: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

{B,D}OM Document Object Model

Document Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 28 / 31

Page 29: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

{B,D}OM Document Object Model

document

document

HTML elementsquerymodificationappending

MethodsgetElementById()

getElementsByTagName()createElement()appendChild()removeChild()

Attributesforms[]

innerHTML

attributestyleonclick

Zsolt Tóth (UM) JavaScript – Introduction 2018 29 / 31

Page 30: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

{B,D}OM Browser Object Model

Table of Contents

1 Introduction

2 SyntaxVariablesControl Flow StatementsFunctions

3 {B,D}OMDocument Object ModelBrowser Object Model

Zsolt Tóth (UM) JavaScript – Introduction 2018 30 / 31

Page 31: JavaScript – Introductionusers.iit.uni-miskolc.hu/~tothzs/edu/webtech/lectures/05JS.pdf · Introduction History 1995 May Netscape Mocha 1995 Sept Mocha !LiveScript 1995 Dec LiveScript

{B,D}OM Browser Object Model

Browser Object Model

Repreents the browsers stateone browser – manydocumentswindow

Historyhistory

navigator

alert()

cookie

Zsolt Tóth (UM) JavaScript – Introduction 2018 31 / 31