Download presentation
Presentation is loading. Please wait.
1
JavaScript Friend or Foe?
2
History of Java and JavaScript Oak and Coffee? ECMAscript European Computer Manufacturers Association JScript?
3
What Side? Server-Side CGI Client-side JavaScript
4
Java vs. JavaScript Java More Complicated Requires JDK Programs compiled and saved as different files Powerful object language JavaScript Easy to learn and use No JDK Scripts right in HTML Simple Tasks
5
Running JavaScript Look on page 8.05 - Recommend you work through this commands Using the HEAD tag to your advantage.
6
Hiding JavaScript <!--hide script from browsers that don’t support javascript all your javascript cool commands //stop hiding from those browsers -->
7
Displaying Text document.write("First statement "); document.write("Second statement "); document.write("Third statement "); document.write(" "); document.writeln("First statement "); document.writeln("Second statement "); document.writeln("Third statement ");
8
Can I quote you on that? Single and double quotation marks for syntax: document.write("Come meet David ‘Bud’ Davis"); All JavaScript commands are case sensitive!!!
9
Variables and Data (8.11) Variable: a named element in a program used to store and retrieve data Given values with assignment operators var Year=2002; document.write(Year); document.write("The year is " + Year);
10
4 Types of Variable Values (Data Types) Number String Boolean variables Null value
11
Declaring a Variable (8.13) Month="December"; var Month; Month=“December”; var Month="December"; Notice the ;
12
Dates (Objects, not getting them) Data Objects: an object containing date information new (keyword) var SomeDay = new Date("June, 15, 2001, 14:25:00"); var SomeDay = new Date(2001, 5, 15, 14, 25, 0); Today = new Date();
13
Extracting Day Information (8.17) Need to now extract the information from Today Use a Method called getDate(); var Day = Today.getDate(); document.write(Day); Day will contain day of the month DateObject (Today) contains complete date and time information
14
Month and Year Information var Today = new Date(); var Month = Today.getMonth()+1 var Today = new Date(); var Year = Today.getYear();
15
The Y2K problem getYear() in Navigator 98, 99, 2000, 2001 in Explorer 98, 99, 100, 101
16
The Solution var Year = Today.getFullYear();
17
Expressions and Operators Expressions -- JavaScript commands that assign values to your variables Operators -- like + operator var ThisMonth = Today.getMonth()+1;
18
Expressions and Operators document.write("Only " + DaysLeft + " days until Christmas"); Look at table on 8.20
19
Operators Binary operators (work on two elements in an operation) Unary operators (work on one variable) Increment operators (increase variable by 1) Decrement operator Negation operator
20
Logical and Comparison Operators Comparison x < 100; y == 20; Logical (x < 100) && (y==20);
21
Logical and Comparison Operators Assignment Operators x = x + y; x += y; Both examples x variable is added to y and stored in x. x = x +2; x += 2;
22
JavaScript Functions (8.22) Way to save time and reuse script function function_name(parameters){ JavaScript Commands } {Command Block}
23
Action Function function ShowDate(date) { document.write("Today is " + date + " "); } -------in Web Page----------- var Today = “04/20/02"; ShowDate(Today);
24
Placing Functions Definitions in : Interpreted before called Browser passes over command block until it is called in the HTML file...
25
Return of JavaScript Creating Arrays Working with Loops and Conditionals
26
Arrays (8.32) An ordered collection of values referenced by a single variable.
27
Weekday var Month = new Array(); Weekday[0]="Sunday"; Weekday[1]="Monday"; Weekday[2]="Tuesday"; Weekday[3]="Wednesday"; Weekday[4]="Thursday"; Weekday[5]="Friday"; Weekday[6]="Saturday";
28
Syntax var Month = new Array(); Month[11]="December"; Automatically Populated with null values
29
Example var Weekday = new Array(); Weekday[0]="Sunday"; Weekday[1]="Monday"; Weekday[2]="Tuesday"; Weekday[3]="Wednesday"; Weekday[4]="Thursday"; Weekday[5]="Friday"; Weekday[6]="Saturday"; var DayofWeek = Today.getDay(); document.write("Today is " + Weekday[DayofWeek]);
30
Loops (8.37) A set of instructions that is executed repeatedly... For While
31
For For(start; stop; update) { JavaScript Commands }
32
For... for(Num=1; Num<4; Num++) { document.write("The value of the Num is " + Num + " "); }
33
The value of the Num is 1 The value of the Num is 2 The value of the Num is 3
34
While while(condition) { JavaScript Commands }
35
While var Num=1 while(Num<4) { document.write("The value of the Num is " + Num); Num++ }
36
For versus While In the example the results are the same You would use a While loop in situations where no counter is available you want to run a loop while a condition exists
37
Conditional Statements [8.28] Runs only if certain conditions are met. if(condition) { JavaScript Commands }
38
Weekend? if(Day=="Friday"){ document.write("Have a nice weekend!"); }
39
Modulus Year % 4 == 0 (Evenly divisible by 4 -- No remainder) if(Year % 4 == 0) { MonthCount[2]=29; }
40
If... Else Structures [8.30] If(condition) { JavaScript Commands if true } else { JavaScript Commands if false }
41
Weekend or Work? if(Day=="Friday"){ document.write("Have a nice weekend!"); } else { if(Day=="Monday") { document.write("Ha-Ha"); } else { document.write("You're either enjoying your weekend or working toward it."); } Note: You'll want string on one line or break string up and use concatenation.
42
Recommendations Work through tutorial File can be downloaded off of resources page--Class/Course Technology Files http://unix.cc.wmich.edu/rea/380/resources/ Ask questions Help one another
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.