JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand...

28
JavaScript Adding active content to websites

Transcript of JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand...

Page 1: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Adding active content to websites

Page 2: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

Goals

Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions to WebPages

Page 3: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Programming language to add interactive content to pages

Validate forms Utilize cookies Control browser functions Inform users

Page 4: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Script should be placed in the <head> section of the page

JS is not HTML, you will need to make the browser aware of the code by using <script> tag <script type="text/javascript"> and

</script>

Page 5: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Rules End each line with a semicolon ; Text must be enclosed in quotes “ “ JS is case sensitive

Upper and lower case letters are NOT the same

JS is incredibly sensitive to mistakes, be extremely cautious!

Page 6: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

<script>document.write("Welcome to my world!!!");</script>

This will write Welcome to my world!!! In your page

Not so exciting, but this can be used to write code to your page, creating massive pages with little coding

Page 7: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

JS uses variables in order to store data and execute code

JS is case sensitive Upper case and lower case are seen

differently Myvariable <> myvariable

<>MyVariable Use same syntax throughout

Page 8: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Popup boxes Not popup windows, that is a breach of

netiquette Alert Confirm Prompt

Page 9: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Alert Used to inform user of something important

Confirm Used to confirm information, typically to

redirect user to acceptable page Prompt

Used to gain information from user for use in document

Page 10: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Alert Alert(“text goes here”)

Confirm Confirm(“text goes here”)

Prompt Prompt(“text goes here”, “default value”)

Page 11: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Confirms are typically written as if statements If statements are standard programming

when a choice needs to be made If OK is selected TRUE is registered If CANCEL is selected FALSE is registered

Prompt boxes are typically used to store information in a variable

Page 12: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript Variable

Place to store information for later use Example

Username=prompt(“Enter Name”, “Name”); Document.write(“Welcome” +username +“!”);

2nd example name=“my first name”; myname=tom; Document.write(myname);

Page 13: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Use the equal sign (=) to assign a value to a variable Variable can be used for input, output

and mathematical operations To compare variable, use a double

equal sign (==)

Page 14: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Other comparing operators

!= Not equal

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

Page 15: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

If statements if (condition) {action1} else {action2}

Exampleif (confirm("Are you home?")) {alert("Welcome Home")}else{alert("Go Away!")};

If statements can also be “nested”if (confirm("Are you home?")) {alert("Welcome Home")}else{if (confirm("Do you want to come in?"))

{alert("Welcome to your new home!")}else{alert("Go Away!")};};

Page 16: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Logical Operators AND ( && ) Both conditions must be true

if (condition && condition) {action}

OR ( || ) Either condition must be true

if (condition || condition) {action}

NOT ( ! ) Inverts result of condition

if (!(condition)) {action}

Page 17: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Functions Pre-set event that allows a script action to take

place on or after specific event JS events will happen as soon as the page

loads unless written in a functionfunction myfunction(){ alert(“Welcome Home!”);}

Later in the page<form name=“myform”><input type=“button” value=“Press Once”onclick=“myfunction()”></form>

Page 18: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

General formfunction functionname(variable1,

variable2…)

{

JS for actual function;

}

Page 19: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Events JS actions are referred to as events

Page 20: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

onmouseover and onmouseout are mainly used in animating buttons Can only be used in conjunction with <a>

tags Idea for use

Create simple images (buttons, arrows, words) for links

onclick will cause an action to happen with user action

Page 21: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Animated Buttons Two steps

onmouseover onmouseout

Animation can only be applied to links

Page 22: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Loops Causes an action to happen a pre-

determined amount of times Two types

For While

Page 23: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

For Used when you know exactly how many

times an action should happen While

Used to do an action until some requirement is met

Page 24: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

For for (variable=startvalue; variable<=endvalue;

variable=variable+incrementfactor) {JS goes here}

Helpful for writing large HTML tables that require user input Idea: prompt box to define variable, for to give

specific range of data

Page 25: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Whilewhile (variable<=endvalue)

{JS goes here}

Also helpful for large tables, better suited when user input is not needed

Page 26: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Break Stops a loop at a specific pointif (value==0) {break};

Continue A skip in a loopfor (value=-50; value<=50; value=value+1)

{if (value==0) {continue};document.write((100/value)+"<br>");}

Page 27: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

Array Automatically define large amount of

variables

Value1=10

Value2=20

Value3=30

….

Value1000=10000

value=new Array;for (number=1; number<=100; number=number+1){ value[number]=number*10};

Page 28: JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.

JavaScript

The name of the array, new Array, is critical Array must be capitalized