Docsity

Introduction to Javascript - Lecture Slides | CGS 3175, Study notes of Computer Science

Partial preview of the text

Download Introduction to Javascript - Lecture Slides | CGS 3175 and more Study notes Computer Science in PDF only on Docsity!

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 1

© Mark Llewellyn

CGS 3175: Internet Applications

Fall 2007

Introduction To JavaScript – Part 3School of Electrical Engineering and Computer Science

University of Central Florida

Instructor :

Dr. Mark [email protected] 236, 407-823-

http://www.cs.ucf.edu/courses/cgs3175/fall

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 2

© Mark Llewellyn

25. Modify

any

of^

the

example

XHTML

documents that illustrate the intrinsic events totry some of the intrinsic events that were notillustrated in the notes such as

onmouseup

Things to Try Yourself

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 4

© Mark Llewellyn

-^

Throughout the semester we have always validated our XHTML documents againstthe strict data type definition (Strict-DTD) to ensure that our XHTML documentswere well-formed.

-^

Some JavaScript statements contain symbols such as the less-than symbol (<), thegreater-than

symbol

(>),

and

the

ampersand

(&).

As

you

become

a^

more

sophisticated JavaScript programmer, you will begin to use many of the featurescontained in the JavaScript language and will undoubtedly encounter the need touse these symbols. Unfortunately, these symbols can prevent XHTML documentsfrom passing validation (particularly under the Strict-DTD).– Note that there is less of a problem with this when using the Transitional-DTD, but wedo not want to relax our standards.

-^

This is not a problem at all when using HTML, because any statements inside a

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 5

© Mark Llewellyn

-^

In contrast, with XHTML documents, the statementsin^

a^

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 7

© Mark Llewellyn

Writing Valid JavaScript Code

Valid JavaScript for compatibility

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 8

© Mark Llewellyn

Writing Valid JavaScript Code

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 10

© Mark Llewellyn

-^

While JavaScript libraries are quite common, it isalso

quite

common

to

see

both

libraries

and

embedded JavaScript code in Web documents, so youneed to be familiar with both forms.

-^

Recall that the

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 11

© Mark Llewellyn

/*^ This

is^ a

JavaScript

library

of^ scripts

//SCRIPT

//this

script

writes

the^

date^

and^ time

onto

a^ page

document.write("

Today

is:^

"^

+^ Date()

+^ "

");

document.write("^ 11){document.write("PM") }^ else

{ document.write("AM") } document.write("^ Hello

there!

I'm

a^ JavaScript

script

executing

on^ your

behalf.

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 13

© Mark Llewellyn

Execution Using AJavaScript LibraryRemember that a JavaScriptlibrary has a “.js” file extension

Execution of SCRIPT #1Execution of SCRIPT #2Execution of SCRIPT #3Execution of SCRIPT #4We’ll see shortly how touse functions to “call” andexecute only one of thesescripts or change theorder of execution orwhatever we want to dowith them.

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 14

© Mark Llewellyn

-^

The values a program stores in computer memory arecalled variables.

Technically speaking, a variable is

actually a specific address in the computer memory.The data stored in a variable often changes.– Think of a variable like a book bag or back pack. You canput any book you want in the bag and retrieve it later foruse. The books in your bag this semester will probably notbe the same ones in your bag next semester.

-^

Many programming languages, such as Java and C++have a very large set of rules that apply to variables.JavaScript is very loose in how variables can be used.

More JavaScript - Variables

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 16

© Mark Llewellyn

Reserved Words In JavaScript

super

in

double

with

static

import

do

while

short

implements

delete

void

public

goto

debugger

var

protected

function

continue

typeof

private

for

const

try

package

float

class

true

null

finally

char

throws

native

false

case

throw

long

extends

byte

this

interface

export

break

synchronized

int

enum

boolean

volatile

return

if

default

transient

new

final

catch

switch

instanceof

else

abstract

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 17

© Mark Llewellyn

-^

Before you can use a variable, you need to declare it (basicallyit means create it).

While there are different techniques to

create

variables,

we’ll

stick

with

the

most

common

and

simplest form which uses the reserved word

var

.

-^

For example, to create a variable named

myVariable

, you

need to write this statement:

var myVariable;

-^

All this statement does is make some memory location beaccessible to your code whenever you refer to it by this name.

Using Variables

myVariable

computer memory

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 19

© Mark Llewellyn

-^

While some variables will be used only internally tothe JavaScript, others will need to be sent to the Webpage for display.

-^

This is quite simple in JavaScript and is basically nodifferent than what we have already been doing withour

simple

scripts

that

have

been

printing

text

(strings) to the Web page.

-^

Writing A Variable’s Value To A Web PageThe next page illustrates a simple XHTML documentwith an embedded JavaScript that uses a variablewhich is sent to the Web page for rendering by thebrowser.

CGS 3175: Internet Applications (JavaScript – Part 3)

Page 20

© Mark Llewellyn

Writing A Variable’s Value To A Web Page - Example Writing a JavaScript variable to a Web page

Thanks for using JavaScript