• No se han encontrado resultados

Main features of JavaScript

N/A
N/A
Protected

Academic year: 2021

Share "Main features of JavaScript"

Copied!
27
0
0

Texto completo

(1)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes y Sistemas Informáticos 1. What is JavaScript? 1.1 Comments 1.2 Data types

1.3 Variables

Chapter 5 - JavaScript

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and 1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types 4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Grupo de Ingeniería del Software y Bases de Datos

Departamento de Lenguajes y Sistemas Informáticos

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 D u rá n T o ro , 201 1

Universidad de Sevilla

March 2013

4.9 HTML DOM 5. Web references © Diseño de Amador D © Diseño de Amador D

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Main features of JavaScript

It is interpreted (

script

language), not compiled

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

It is an object-oriented language

It is not Java, although its syntax is similar

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and 1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types 4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM

5. Web references Amador

(2)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes y Sistemas Informáticos

Comments in JavaScript

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

//

One line comment

/*

Several line comments

are the same as in Java

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

are the same as in Java

*/

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types 4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

There may be comments of one line or several lines.

They have the same syntax as in Java or C++.

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM 5. Web references © Diseño de Amador D

March 2013

Introducción a la Ingeniería del Software y a los Sistemas de Información

2

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Basic data types in JavaScript

––

Strings

Strings: "Hello, world!", '[email protected]'

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

––

Numbers

Numbers: 12, 22.4, -5

––

Booleans

Booleans: true, false

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

String constants

“JavaScript is 'cool' "

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

“JavaScript is 'cool'."

“You must be careful with quotes'

'C

I

h1 HTML /h1

t

?'

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

'Can I use <h1>HTML</h1> tags?'

"Can I use the slash: \\ ?"

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM 5. Web references de Amador D

(3)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Declaration of variables in JavaScript

JavaScript is case sensitive, which means variable

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

name

is not the same as variable

Name

.

It is not mandatory to declare variables, but it is

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

recommended.

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

var IISSImark;

IISSImark = 10;

// No explicit declaration

IISSImark = 10;

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

I iti li

ti

f

i bl

NOT RECOMMENDED

NOT RECOMMENDED

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Initialization of variables

var Pract = 10, Test = 10;

var Total

(Pract*0 4)+(Test*0 6);

var ISSImark;

IISSImark

10;

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

var Total = (Pract*0.4)+(Test*0.6);

IISSImark = 10;

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

4 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Functions in JavaScript

function function_name( parameters )

{

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

{

var local_variable = initial_value;

// function code

t

(

t

l

)

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

If there is no

return

return

the function returns an

undefined

return (return_value);

}

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

If there is no

return

return

the function returns an

undefined

variable

.

There are three predefined functions that are very

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

There are three predefined functions that are very

useful to validate forms:

••

parseInt

parseInt: converts a string to an integer.

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

p

p

g

g

••

parseFloat

parseFloat: converts a string to a float.

••

isNaN

isNaN: it is true whether the parameter is not a number.

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

parseInt( "456" ) == 456; // true

parseFloat( "1.34" ) == 1.34; // true

i N N(

I t( “

" ) ) // t

4.9 HTML DOM

5. Web references Amador

D

(4)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Control structures in JavaScript

They have the same syntax as in Java and C++.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

switch (expression) {

case label1 :

// bl

k 1

if (condition) {

//

if

block

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

// block 1

break;

case label2 :

}

else {

//

else

block

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

// block 2

break;

//

else

block

}

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

...

default :

// block n

while (condition) {

//

while

block

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

}

for (i=0; i<=N; i++) {

}

d {

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

for (i=0; i<=N; i++) {

//

for

block

}

do {

//

do

block

} while (condition)

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

6 March 2013

Javascript

C

diti

l

i

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Conditional expressions

if (condition) {

while (condition) {

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

if (condition) {

/* Code executed if the

condition is true */

}

/* Code executed if while

condition is true */

}

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

}

else {

/* Code executed if the

}

do {

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

/ Code executed if the

condition is false */

}

do {

/* Code executed if the

condition is true */

}

hil (

diti

)

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes

4 3 Node types

Condition examples

} while (condition)

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Condition examples

(IISSIpassed) is

true

(notInitialized) is

false

var IISSIpassed;

var notInitialized;

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

(notInitialized) is

false

(IISSImark >= 5) is

true

(IISSImark == 5) is

false

var IISSImark = 10;

IISSIpassed = true;

4.9 HTML DOM 5. Web references de Amador D

(IISSImark

5) is

false

(5)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript classes

Classes define categories of objects.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

For instance: Car, Person, String, …

Objects in JavaScript

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

Objects in JavaScript

An object represents one instance of a class.

For instance

joe

and

mike

are

var joe = new Person();

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

For instance,

joe

and

mike

are

instances of the class Person.

Objects are associative arrays to which new properties

j

();

var mike = new Person();

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

Objects are associative arrays, to which new properties

or functions (methods) can be dynamically added.

Properties can be accessed as follows:

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Properties can be accessed as follows:

object.property

object[“property"]

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

object[ property"]

Methods can be accessed as follows:

bj

t

th d(

t

)

4.9 HTML DOM 5. Web references © Diseño de Amador D

object.method( parameters )

Introducción a la Ingeniería del Software y a los Sistemas de Información

8 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript has several predefined classes

The most commonly used are: Array

Array, Boolean

Boolean, Math

Math,

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Date

Date, Number

Number, Object

Object and String

String.

They can be used to create new instances:

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

var str1, str2;

str1 = new String(“My String");

1.7 Classes and

objects 1.8 Predefined classes

2. JavaScript and the web

str2 = str1.toUpperCase();

// str2 = “MY STRING"

3. BOM

4. DOM

4.1 The DOM tree 4.2 DOM nodes

4 3 Node types

Or also

statically

:

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Or also

statically

:

var str;

str

String(“Static") toUpperCase();

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

str = String(“Static").toUpperCase();

// str = "STATIC"

4.9 HTML DOM

5. Web references Amador

(6)

Predefined classes String

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined classes: String

Properties

length string length

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

length: string length.

Methods

big(): sets the font bigger

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

big(): sets the font bigger

bold(): sets the font in bold

charAt(n): returns the character at position

n

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

match(c) : returns whether substring c belongs to the

string

substring(x,y): returns the substring from

x

to

y

both

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

g( ,y)

g

y

included (strings start in 0)

toLowerCase(): converts to lower case

toUpperCase(): converts to upper case

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

toUpperCase(): converts to upper case

valueOf() : returns the value of type string (important

when comparing strings)

i

()

h

bj

i

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

toString(): converts the object to a string

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

10 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined classes: Math

Methods

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Methods

abs(number): returns the absolute value

max(x y): returns the maximum between

x

and

y

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

max(x,y): returns the maximum between

x

and

y

min(x,y): returns the minimum between

x

and

y

pow(base exp): pow

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

pow(base, exp): pow

random(): returns a random number in [0,1]

d(

b

)

d t th

l

t i t

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

round(number): rounds to the closest integer

sin(number): function sin

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

sqrt(number): function square root

tan(number): function tangent

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

4.9 HTML DOM 5. Web references de Amador D

(7)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined classes: Array

Properties

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

length: length of the array

Methods

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

Methods

join(): joins the elements separating them with commas.

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

reverse(): inverts the order of the elements

sort(): sort the elements of the array

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

a = new Array (25); // creates an array with 25 elements

a = new Array (1,“iissi",true); // an array with 3 elements

a = new Array (“mystring"); // an array with one string

4.3 Node types

4.4 Elements 4.5 Traversing the tree

4.6 Finding

elements in the

Array with 3 rows and 6 columns?

a = new Array ( mystring ); // an array with one string

a = new Array (false); // an array with the element false

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

Array with 3 rows and 6 columns?

a = new Array (3);

for (i=0;i<3;i++) a[i]= new Array (6);

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

12

//array access

a[i][j]

March 2013

Javascript

P

d fi

d l

D t

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined classes: Date

Methods

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

getDate(): returns the day of the current month as an

integer between 1 and 31

tD

()

t

th d

f th

t

k

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

getDay(): returns the day of the current week as an

integer between 0 and 6

getHours(): returns the hour of the current time as in

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

getHours(): returns the hour of the current time as in

integer between 0 and 23.

getMinutes(): returns the minutes of the current time as

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

an integer between 0 and 59.

getMonth(): returns the month of the current year as an

integer between 0 and 11

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

integer between 0 and 11.

getSeconds(): returns the seconds of the current minute

as an integer between 0 and 59.

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

g

getTime(): returns the time in milliseconds elapsed from

the 1st January 1970

4.9 HTML DOM

5. Web references Amador

(8)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined classes: Date (cont.)

Methods

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

getYear(): returns the current year as an integer

setDate(day, month): sets the day and month in the

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

se

a e(day,

o

) se s

e day a d

o

e

object Date in use.

toGMTString(): returns a string with the current date

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

g()

g

(GMT zone)

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

var f = new Date ();

var f = new Date (year, month);

var f = new Date (year, month, day);

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

var f new Date (year, month, day);

var f = new Date (year, month, day, hours);

var f = new Date (year, month, day, hours, minutes);

var f = new Date (year month day hours minutes seconds);

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

var f = new Date (year, month, day, hours, minutes, seconds);

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

14 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined classes: Date (cont.)

function dayOfTheWeek()

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

{

var today = new Date();

var day;

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

var day;

switch ( today.getDay() ) {

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

case 0: day = “Sunday";

break;

case 1: day = “Monday";

break;

2 d

“T

d

"

b

k

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

case 2: day = “Tuesday";

break;

case 3: day = “Wednesday";

break;

case 4: day = “Thursday"; break;

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

y

y ;

;

case 5: day = “Friday";

break;

case 6: day = "Saturday";

break;

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

}

return( day );

}

4.9 HTML DOM 5. Web references de Amador D

}

(9)

i

i

i

l

lik

h

Escuela Técnica Superior

de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript is a programming language like any other

and it can be used in many different contexts.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

However, it has a widespread use in the web where

it is used to give dynamism to web pages.

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

When used inside a web browser,

there are several available

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

predefined objects

Some objects are related to the

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

j

own web browser (BOM – Browser Object Model)

window.alert(“¡que viene IISSI!");

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Others are related to the structure of the HTML

document (DOM) and can be accessed by means of

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

the object

document

document

:

<img id="imgmail" src="mail.jpg"/>

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

16

document.getElementById("imgmail").src = "mail.jpg";

March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes y Sistemas Informáticos

JavaScript in context

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

h

l

Internet

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

http://

http://server com/page html

server com/page html

server.com

server.com

page.html

Internet

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

http://

http://server.com/page.html

server.com/page.html

se

se

e co

e co

<html>

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<html>

<head>…</head>

<body>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<h1>Page</h1>

</body>

/h

l

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

</html>

page.html

4.9 HTML DOM

5. Web references Amador

(10)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript in context (cont.)

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Internet

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

http://

http://server com/page html

server com/page html

server.com

server.com

Internet

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

http://

http://server.com/page.html

server.com/page.html

se

se

e co

e co

Page

<html>

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

Page

<html>

<head>…</head>

<body>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

y

<

<h1>Page</

h1>Page</h1>

h1>

</body>

/h

l

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

</html>

page.html

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

18 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript in context (cont.)

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

h

l

Internet

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

http://

http://server com/page html

server com/page html

server.com

server.com

page.html

Internet

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

http://

http://server.com/page.html

server.com/page.html

se

se

e co

e co

<html>

<head>…</head>

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<body>

<h1>Page</h1>

i t

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<script>

document.Write("Hello World!");

</script>

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

/script

</body>

</html>

4.9 HTML DOM 5. Web references de Amador D

page.html

(11)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript in context (cont.)

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Internet

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

http://

http://server com/page html

server com/page html

server.com

server.com

Internet

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

http://

http://server.com/page.html

server.com/page.html

se

se

e co

e co

<html>

<head>…</head>

Page

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<body>

<h1>Page</h1>

i t

Page

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<script>

document.Write("Hello World!");

</script>

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

/script

</body>

</html>

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

20

page.html

March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

JavaScript in context (cont.)

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Internet

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

http://

http://server com/page html

server com/page html

server.com

server.com

Internet

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

http://

http://server.com/page.html

server.com/page.html

se

se

e co

e co

<html>

<head>…</head>

Page

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<body>

<h1>Page</h1>

i t

Page

Hello World!

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<script>

document.Write("Hello World!");

</script>

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

/script

</body>

</html>

4.9 HTML DOM

5. Web references Amador

D

(12)

Wh

d

J

S i t?

Escuela Técnica Superior

de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Where do we use JavaScript?

In the HTML document

Inside the element

head

1. What is JavaScript?

1.1 Comments 1.2 Data types 1.3 Variables

Inside the element

head

Inside the element

body

As an event handler onevent=“code"

*

1.4 Functions

1.5 Structures of control 1.6 Conditionals 1.7 Classes and

As the URL of a link href="javascript:code"

**

In a separated file

J

S i t

d

ithi

l

t

i t

h

ld b

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

JavaScript code within an element

script

should be

placed inside HTML comments to avoid problems

with old web browsers

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

with old web browsers.

In XHTML it must be enclosed in a section

CDATA

.

I

hi h

d

i J

S i t

d

t d?

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

In which order is JavaScript code executed?

JavaScript code is always executed in the same order as in the

HTML code.

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

Usually, element

head

contains only declarations of variables and

functions.

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

22

*

Events and their handling are studied in the next lesson.

**

NOT RECOMMENDED.

March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Script inside the HTML document (

body

)

<html>

<head>

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

<head>

<title>Document title</title>

<!– other header information -->

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

</head>

<body>

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

<!-- document content -->

<script type="text/javascript">

3. BOM

4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<!--// JavaScript code

// >

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding

elements in the

<! more document content

>

//-->

</script>

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

<!– more document content -->

</body>

</html>

4.9 HTML DOM 5. Web references de Amador D

/

(13)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Script inside the HTML document (

head

)

<html>

<h

d>

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

<head>

<title>Document title</title>

<script type="text/javascript">

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

<script type= text/javascript >

<![CDATA[

// JavaScript code

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

]]>

</script>

3. BOM

4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<!-- other header information -->

</head>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<body>

<!-- document content -->

</bod >

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

</body>

</html>

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

24 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Script in a separated file

<html>

<h

d>

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

<head>

<title>Document title</title>

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

<script type="text/javascript" src=“mycode.js">

</script>

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

<!– other header information -->

</head>

<body>

3. BOM

4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<body>

<!– document content -->

</body>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

</html>

// JavaScript

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

// code

4.9 HTML DOM

5. Web references Amador

D

y

j

(14)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Browser Object Model (BOM)

window: the window of the navigator.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

screen: the screen.

navigator: the web browser itself.

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

history: the navigation history.

location: the URL of

window

navigator

location

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

the current page.

oca o

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types 4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

26 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: window

Represents the window of the navigator

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

status: status bar message

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

status: status bar message

document: current HTML document

history: navigation history

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

history: navigation history

closed: true if the window is closed

l

th

b

f f

th t

t i

d i th

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

length: number of frames that are contained in the

window

frames an arra that contains all frames that are

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

frames: an array that contains all frames that are

contained in the window, i.e., returns an array of

objects of type Frame

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

objects of type Frame

4.9 HTML DOM 5. Web references

de

Amador

(15)

P

d fi

d bj

i d

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: window

Methods

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

alert(msg): shows a window with a message

confirm(msg): shows a window with

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

g

an accept and a cancel button;

it returns true if accept has been

d

d f l

if

l h

b

d

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

pressed and false if cancel has been pressed.

prompt(msg,default): shows a window that prompts

a string with a

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

a string with a

default value

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

open(url,name,opt): opens a

new window in the web browser

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

close(): closes the window

navigate(url): go to url (only IE)

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

28 March 2013

Javascript

P

d fi

d bj

t

i

t

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: navigator

Represents the navigator itself.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

appName: name of the navigator: Microsoft Internet

E

l

M

ill

N tS

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

Explorer, Mozilla, NetScape, …

appVersion: Netscape 5.0 (Windows; es-ES) Microsoft

Internet Explorer 4 0 (compatible; MSIE 6 0; Windows NT

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

Internet Explorer 4.0 (compatible; MSIE 6.0; Windows NT

5.0)

mimeTypes: an array with the MIME types (

Multipurpose

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

Internet Mail Extensions

) supported by the navigator

(

Application, Audio, Image, Message, Multipart, Text,

Video

)

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Video

)

plugins: an array with the plugins that are installed in the

navigator

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

javaEnabled(): returns a boolean indicating whether the

navigator supports Java applets

4.9 HTML DOM

5. Web references Amador

(16)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: frame

Represents a frame.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

parent: the parent frame

1.4 Functions 1.5 Structures of

control 1.6 Conditionals

1.7 Classes and

Predefined objects: location

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

Represents the URL of the document displayed in

window

Properties

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

p

host: Contains the server name and the port number of the URL

hostname: server name

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

pathname: path without including the server name and the port

number

t

t

b

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

port: port number

protocol: protocol (http, ftp,..)

href: URL that can be dynamically changed

4.9 HTML DOM 5. Web references © Diseño de Amador D

href: URL that can be dynamically changed

Introducción a la Ingeniería del Software y a los Sistemas de Información

30 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

What is DOM?

Designed to access to XML and HTML documents

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Designed to access to XML and HTML documents

W3C standard ( http://www.w3.org/DOM/

)

Al

k

W3CDOM

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

Also known as W3CDOM

Three levels:

level 1, level 2, level 3

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

Each navigator implements a version of DOM

From IE 6 (Trident) and browsers based on Gecko

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

(Mozilla, Firefox) support W3CDOM

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Trident

Gecko

KHTML

Presto

iCab

L1

6.0

1.0

Yes

7.0

Yes

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

L2

Minor

Major

Major

Major

Major

L3

No

Minor

Minor

Minor

Minor

4.9 HTML DOM 5. Web references

de

Amador

(17)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

The

The DOM

DOM tree

tree

Menú 10-02-2012

document

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

<html>

<head>

Primeros •Pasta •Ensalada Segundos

head

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

document

(html)

<title>Menú Cafeta</title>

</head>

b d

Segundos •Lomo •Flamenquin

title

body

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

head

title

”Menú Cafeta”

<body>

<h1>Menú 10-02-2012</h1>

Primeros

<ul>

ul

body

h1

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

body

h1

”Menú 10-02-2012”

<ul>

<li>Pasta</li>

<li>Ensalada</li>

</ul>

li

li

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

ul

”Primeros”

/

Segundos

<ul>

<li>Lomo</li>

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

li

”Pasta”

li

<li>Flamenquin</li>

</ul>

</body>

4.9 HTML DOM 5. Web references © Diseño de Amador D

”Ensalada”

Introducción a la Ingeniería del Software y a los Sistemas de Información

32

</html>

March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

DOM

DOM nodes

nodes

document

(html)

Elements

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

head

title

”Menú Cafeta”

Elements

body

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

body

h1

”Menú 10-02-2012”

h1

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

ul

”Primeros”

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

li

”Pasta”

li

Text

”Menú 10-02-2012”

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

”Ensalada”

”Primeros”

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM

5. Web references Amador

(18)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes y Sistemas Informáticos

Node types

Node types

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

…<div id=“

…<div id=“MyDiv

MyDiv”>DOM

”>DOM iis easy</

s easy</div>…

div>…

1.4 Functions 1.5 Structures of

control 1.6 Conditionals

1.7 Classes and

Element

Element

Attribute

Attribute

Text

Text

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

Elements

Represent an HTML tag

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

Represent an HTML tag

Attributes

div

”M Di ”

id

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

Provide access to the attributes

of a tag.

”DOM is easy”

”MyDiv”

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

Text

Represents strings.

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

34 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

Elements

Elements

• Properties

Properties

IMG

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

– tagName (

READ ONLY

)

• Contains the name of the element’s tag

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

• Methods

Methods

– getAttribute(n)

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

g

( )

• Returns the attribute with name “n”

– setAttribute(n,v)

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

( , )

• Sets the value of attribute “n” to “v”

<script type="text/javascript">

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<img src=“homer.jpg”/>

<img src=“homer.jpg”/>

p

yp

/j

p

<!—

// …

// image contains the object of the element

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

alert(image.tagName);

image.setAttribute(“src”,”bart.jpg”);

//-->

4.9 HTML DOM 5. Web references de Amador D

</script>

(19)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

Traversing

Traversing the

the tree

tree

The

The array

array c

childNodes

childNodes gives

gives

access

access to

to the

the child

child nodes

nodes of

of

element

1. What is JavaScript?

1.1 Comments 1.2 Data types

1.3 Variables

The

The object

object d

document.body

document.body

access

access to

to the

the child

child nodes

nodes of

of

an

an element

element..

subelem1

subelem2

element.childNodes[0];

element.childNodes[1];

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

provides

provides direct

direct access

access to

to

the

the document

document body

body

subelem3

element.childNodes[2];

1.7 Classes and

objects 1.8 Predefined classes

2. JavaScript and the

web

<script type="text/javascript">

document

body

3. BOM

4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<!--var e1 = document.body.childNodes[0];

var root = document.body;

h1

”Menú 10-02-2012”

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

var e2 = root.childNodes[1].childNodes[0];

var list = root.childNodes[1];

li t hildN d

[1] hildN d

[0]

Menú 10 02 2012

ul

li

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

list.childNodes[1].childNodes[0];

//-->

</script>

li

”Pasta”

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

36

</script>

li

”Ensalada”

March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

Finding

Finding elements

elements in

in the

the tree

tree

There

There are

are two

two ways

ways::

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

• By

By means

means of

of the

the tag

tag name

name

– getElementsByTagName(tagName)

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

g

y

g

(

g

)

<script type="text/javascript">

<!--1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

var images = document.getElementsByTagName(“img”);

var firstImage = images[0];

//-->

</script>

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

• By

By means

means of

of the

the id of

id of the

the element

element

tEl

tB Id(id)

</script>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

– g

getElementById(id)

<script type="text/javascript">

<!--elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

<!--var div = document.getElementById(“mainDiv”);

//-->

</script>

4.9 HTML DOM

5. Web references Amador

D

(20)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

Finding

Finding elements

elements in

in the

the tree

tree ((

examples

examples

))

1. What is JavaScript? 1.1 Comments 1.2 Data types

1.3 Variables

<script type="text/javascript">

<html>

<head>

<title>Menú Cafeta</title>

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

<script type= text/javascript >

<!--var items = document.getElementsByTagName(“li”);

var myItem = items[1];

<title>Menú Cafeta</title>

</head>

<body>

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

y

[ ];

var myDiv = document.getElementById(“primeros”);

var list = myDiv.getElementsByTagName(“ul”)[0];

<h1>Menú 10-02-2012</h1>

<div id=“primeros”>

Primeros

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

var items2 = list.getElementsByTagName(“li”);

//-->

<ul>

<li>Pasta</li>

<li>Ensalada</li>

l

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

</script>

</ul>

</div>

<!-- … -->

</body>

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

</body>

</html>

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

38 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

Modifying

Modifying the

the DOM

DOM tree

tree

• All

All nodes

nodes have

have an

an interface

interface to

to

insertBefore

insertBefore

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

• All

All nodes

nodes have

have an

an interface

interface to

to

modify

modify the

the structure

structure of

of the

the tree

tree

((i.e.

i.e. add

add nodes

nodes,

, remove

remove nodes

nodes…)

…)

replaceChild

replaceChild

removeChild

removeChild

appendChild

appendChild

1.4 Functions 1.5 Structures of control 1.6 Conditionals

1.7 Classes and

• To

To create

create new

new elements

elements you

you must

must

createElement

createElement

1.7 Classes and

objects 1.8 Predefined classes

2. JavaScript and the web

• To

To create

create new

new elements

elements,

, you

you must

must

use

use the

the parent

parent node

node ((d

document

document))

..

createElement

createElement

createTextNode

createTextNode

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<script type="text/javascript">

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<!--var newTextNode;

newTextNode = document.createTextNode("Hello World! ");

d

b d

dChild(

T

N d )

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

document.body.appendChild(newTextNode);

//-->

</script>

4.9 HTML DOM 5. Web references de Amador D

(21)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

Event

Event handling

handling (L2)

(L2)

<html>

<head>

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

<head>

<title>Document title</title>

</head>

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

<body>

<!-- document content -->

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

<marca … onevent="JavaScript code">

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

<!– more document content -->

</marca>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

</body>

</html>

It is usually a call to a

function that has been

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

function that has been

previously declared in a

script

block in either the

section

head

or in an

4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

40

external file

section

head

or in an

external file.

March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

••

HTML DOM

HTML DOM

• It

It is

is an

an extension

extension to

to the

the basic

basic DOM

DOM

1. What is JavaScript? 1.1 Comments 1.2 Data types

1.3 Variables

Document

Form[]

1.4 Functions 1.5 Structures of

control 1.6 Conditionals

1.7 Classes and

Plugin[]

Textarea

Text

FileUpload

Password

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

Applet[]

Anchor[]

Text

Password

Reset

Radio

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

Anchor[]

Area[]

Hidden

Checkbox

Button

Select

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding

elements in the

Link[]

Image[]

Submit

Options[]

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

Layer[]

4.9 HTML DOM

5. Web references Amador

(22)

P

d fi

d bj

d

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: document

Represents the document that is being displayed.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

bgColor: integer value with the code of the background

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

g

g

g

color or string with the color ("#12345", "red")

fgColor: identifies the document text color

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

forms: an array that contains all elements

form

of the

document

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

images: array that contains all images in the

document

lastModified: string with the last modification date

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

lastModified: string with the last modification date

links: array that contains all links in the document

referrer: URL of the link from which the user reached

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1

referrer: URL of the link from which the user reached

the document.

title: string with document title

4.9 HTML DOM 5. Web references © Diseño de Amador D

title: string with document title

Introducción a la Ingeniería del Software y a los Sistemas de Información

42 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: document (cont.)

Methods

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Methods

close(): closes the document (important to do it in

written)

1.4 Functions 1.5 Structures of control 1.6 Conditionals 1.7 Classes and

written)

write(str), writeln(str): modifies the document (notice

that writeln does not insert a new line when the

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

that writeln does not insert a new line when the

document is displayed in the navigator)

getElementById(id): returns the element with the

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

g

y ( )

specified ID

<img id="imgmail" src="mail jpg"/>

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the

<img id= imgmail src= mail.jpg />

document.getElementById("imgmail").src = "mail.jpg";

elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM 5. Web references de Amador D

(23)

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: link

Represents the links.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

target: string with the target name

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

target: string with the target name

href: string with the URL of the link

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types 4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM 5. Web references © Diseño de Amador D

Introducción a la Ingeniería del Software y a los Sistemas de Información

44 March 2013

Javascript

Escuela Técnica Superior de Ingeniería Informática Departamento de Lenguajes

y Sistemas Informáticos

Predefined objects: image

Represents the images.

1. What is JavaScript? 1.1 Comments 1.2 Data types 1.3 Variables

Properties

border: integer with the value of the

border

of the

1.4 Functions 1.5 Structures of

control 1.6 Conditionals 1.7 Classes and

border: integer with the value of the

border

of the

image (in pixels)

complete: boolean that indicates whether the image

1.7 Classes and objects 1.8 Predefined classes

2. JavaScript and the web

complete: boolean that indicates whether the image

has been fully loaded from the server

src: it refers to the content of the image itself

3. BOM 4. DOM

4.1 The DOM tree 4.2 DOM nodes 4 3 Node types

src: it refers to the content of the image itself

4.3 Node types 4.4 Elements 4.5 Traversing the tree 4.6 Finding elements in the elements in the tree 4.7 Modifying the tree 4.8 Event handling 4 9 HTML DOM Du rá n T o ro , 201 1 4.9 HTML DOM

5. Web references Amador

Referencias

Documento similar