syst 28043 web technologies syst 28043 web technologies lesson 6 – intro to javascript

29
SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

Upload: bryce-mccoy

Post on 28-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

SYST 28043

Web Technologies

SYST 28043

Web Technologies

Lesson 6 – Intro to JavaScript

Page 2: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 2

JavaScriptJavaScript

JavaScript is NOT Java!The syntax is similar

JavaScript is a scripting languageClient-sideObject OrientedHas control structures, variables, etc.

Page 3: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 3

Using JavaScriptUsing JavaScript

In-Line JavascriptPut JavaScript in document bodyWhere you want results to appear

In <head> section of documentGreat for functions

In external .JS fileStore all your JavaScript in one or more filesRefer to it in document when needed

Page 4: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 4

<script></script> Tag<script></script> Tag

Used to put JavaScript in your document header or bodyAttributes:

type=“text/javascript”language=“javascript”

Deprecated

src=“myscripts.js”

Page 5: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 5

<script></script> Tag<script></script> Tag

<script>

<!--

// my statements and functions

// go here

// -->

</script>

The comments keep script hidden from older browsers

Page 6: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 6

In-Line ScriptsIn-Line Scripts

Put the script in <script> tags where you want the output to appear

<p>Page last modified on:

<script type="text/javascript">

<!--

document.write(document.lastModified);

// -->

</script>

</p>

Page 7: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 7

Scripts in <head>Scripts in <head>

You can put scripts in the <head> section

If you want to refer to them more than once in your documentIf they contain functions used in other scripts in your document

Use the <script> tag in the <head> tags

Page 8: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 8

Scripts in <head>Scripts in <head><head><title>Kaluha's Cat House</title><script type="text/javascript"><!-- function displayMsg() { alert("Welcome to the Cat House!"); }// --></script></head>

Page 9: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 9

External JavaScript FilesExternal JavaScript Files

Useful if you have many functionsUseful when you use functions in more than one pageJust type the scripts in plain text file

Give the file a .js extensionUsually saved in a /scripts sub-directory

I’ll show you an example!

Page 10: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 10

JavaScript Coding BasicsJavaScript Coding Basics

Variables & Data TypesOperatorsControl StructuresFunctions

Page 11: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 11

Variables & Data TypesVariables & Data Types

Variables can be declaredIt’s good practice

Use the var keywordYou don’t have to specify data type

var userName="";

var dateFlag = 1;

Page 12: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 12

Variables and Data TypesVariables and Data Types

Rules for Identifiers:identifier names are case-sensitiveidentifiers must start with a letteridentifier names may not contain spacesidentifiers must be made up of letters and numbersthe only symbols allowed in identifier names are the underscore and $

Page 13: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 13

Variables & Data TypesVariables & Data Types

\b backspace

\f Form feed

\n New-line

\r Carriage return

\t Tab

\’ Single-quote

\” Double-quote

\\ Backslash

alert("I said \"Click the button first!\" silly!");

document.write("Your location should be:\n\tC:\\webtech");

Escape

Sequences

Page 14: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 14

OperatorsOperators

Arithmetic Operators+ addition- subtraction* multiplication/ division% modulus++ unary increment-- unary decrement

Page 15: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 15

OperatorsOperators

Assignment Operators= equals+= plus-equals-= minus-equals*= multiply-equals/= divide-equals%= modulus-equals

Page 16: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 16

OperatorsOperators

Relational Operators== equal to!= not equal to> greater than>= greater than or equal to< less than<= less than or equal to

Logical Operators&& AND Operator|| OR Operator! NOT Operator

Page 17: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 17

Control StructuresControl Structures

If-Statementsif (condition){

// code body

}

if (condition){

// code body

} else {

// code body

}

Page 18: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 18

Control StructuresControl Structures

Switch Statementswitch (expression) {case value1:

// code bodybreak;

case value2:// code bodybreak;

default:// code body

}

Page 19: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 19

IterationIteration

Pre-Test and Post-Test Loop

while(condition) {

// code body

}

do {

// code body

} while (condition);

Page 20: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 20

IterationIteration

For Loops:

for (init; condition; cont) {

// code body

}

for (item in collectionType) {

// code body

}

Page 21: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 21

Defining FunctionsDefining Functions

Function headerKeyword functionFunction nameParameter list in brackets

Or empty brackets if no paramsParams don’t need data types

Return valuesJust add the return statement in the function

Page 22: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 22

Defining FunctionsDefining Functions

function functionname(param1, param2, ...)

{

// function body

return value; // optional

}

Variable ScopeVariables defined in a function are local to that functionDefine public variables outside functions

Page 23: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 23

ExercisesExercises

Do the 3 quick exercises in the notes

Page 24: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 24

JavaScript ObjectsJavaScript Objects

There are some pre-defined objects in JavaScriptThey include (these aren’t all of them)

DateStringMathArray

We’ll look at the references for these on-line

Page 25: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 25

ExercisesExercises

Do the three exercises in the notes that use JavaScript objects

Page 26: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 26

DOMDOM

Document Object ModelW3C HTML standard for the structure of HTML documentsObjects:

WindowDocumentLocationHistory

Each has various methods and properties you can use

Page 27: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 27

Event HandlingEvent Handling

Many elements and DOM objects have eventsEvents = user manipulation of element/control/object

You can associate code with an eventWhen the event occurs, the code executes

E.g. when user clicks a button, code in the “Click” event will execute

See Event Reference online

Page 28: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 28

Common TasksCommon Tasks

The notes on-line take you through some common, simple JavaScript tasks:

Using Dialog BoxesMaking a “Back” linkRedirecting the UserWorking with Date/TimeForm ValidationImage Rollovers

Page 29: SYST 28043 Web Technologies SYST 28043 Web Technologies Lesson 6 – Intro to JavaScript

04/19/23 Wendi Jollymore, ACES 29

ExerciseExercise

Go through the Common Tasks and try them all out

We’ll do some of them together