1 hci 201 javascript - part 1. 2 static web pages l static pages: what we have worked with so far l...

30
1 HCI 201 JavaScript - Part 1

Post on 19-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

1

HCI 201

JavaScript - Part 1

Page 2: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

2

Static web pages

Static pages: what we have worked with so far

HTML tags tell the browser what to do with the content

Pages don’t change during viewing

Like reading a book or magazine

Page 3: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

3

Dynamic web pages

Display or behavior can change during viewing

Can prompt for entry of information

Can store information in memory

Display can incorporate user’s input or the result of computations

A fun example

A fancy example

Page 4: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

4

What is JavaScript?

JavaScript is NOT Java …more details… JavaScript is an interpreted web scripting

language run from the Web browser. JavaScript is the scripting language of

choice for creating interactive web pages.

Page 5: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

5

Key differences Java/JavaScript

Java is a full-blown programming language; JavaScript is a limited scripting language.

Java is relatively complex; JavaScript is usually easier to learn.

Java must be compiled and interpreted; JavaScript needs only to be interpreted.

Java makes larger files and is slower on the Internet; JavaScript creates smaller files and is faster.

Java can run without a browser; JavaScript requires a browser... it works only in a WWW environment.

Java Applets are applications designed specifically for the WWW.

Page 6: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

6

JavaScript is coded within HTML documents

<html>

<!-- lecture 5 IT-130 -->

<head> <title> JavaScript example </title> </head>

<body>

<script type="text/javascript">

yourname = prompt("Please enter your name", "");

document.write("Hello " + yourname + ", and welcome !");

</script>

<p> I hope you will enjoy learning JavaScript. </p>

<hr>

<img src="javascript.jpg">

</body>

</html>

Download pic to try this example. javascript.jpg

This script is in the html body

Page 7: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

7

Prompt window

Page 8: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

8

Page 9: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

9

Look at the script closer…

yourname = prompt("Please enter your name", "");

document.write("Hello " + yourname + ", and welcome !");

Page 10: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

10

Prompt statement

yourname = prompt("Please enter your name", "");

Your message

Page 11: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

11

Prompt statement

yourname = prompt("Please enter your name", "");

Default entry value

Page 12: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

12

Prompt statement

yourname = prompt("Please enter your name", “Joe");

Default entry value

Page 13: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

13

Write statement

document.write("Hello " + yourname + ", and welcome !");

Forms a text string given to the browser for processing

Page 14: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

14

Write statement

document.write("Hello " + yourname + ", and welcome !");

Forms a text string given to the browser for processing

+ concatenates (joins) strings and variables

Page 15: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

15

Write statement

document.write("Hello <i>" + yourname + "</i>, and welcome");

Forms a text string given to the browser for processing

+ concatenates (joins) strings and variables

String can contain HTML tags!

Page 16: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

16

Common errors…

document.write("Hello " + yourname + ", and welcome !");

Missing quote marks

Misspelled reserved words

With Internet Explorer, turn on detailed error reporting with: Tools > Internet Options > Advancedand click on: Display a notification about every script error

Page 17: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

17

Keep in mind …

JavaScript is case sensitive. Each JavaScript command line must end

with a semi-colon (;) One-line comment: // comment Multi-line comment: /* comment */

Page 18: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

18

Variables

A variable is a named element in a program, used to store and retrieve information

Name must start with a letter

Case sensitive: “A” and “a” are different!

No space allowed in name

No “special characters” like $ or # or @

Underscore is OK to join parts of name.

Page 19: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

19

Variables

When you have more than one word in a variable name, start with a lowercase first letter and capitalize the first letter of any subsequent word. Ex: currentDate

Page 20: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

20

Valid variable names

tempInFahr

temp_in_fahrenheit

TEMP_IN_FAHR

SUM

current_age

Sum2Date

Page 21: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

21

Invalid variable names

$pay_amount

2hotForU

Count#

count of widgets

final

$ not valid in name

Can’t start with a digit

# not valid in name

Reserved word

Blanks in name

Page 22: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

22

Reserved words (Pg. 63)

Page 23: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

23

Hiding JavaScript from old browsers

Within the <script> tag, include HTML comment lines such as:<script language=“JavaScript”>

script commands and comments

</script>

<!- - Hide this script from browsers that don’t support JavaScript// Stop hiding from other browsers - ->

When a Web browser that doesn’t support scripts encounters this code, it ignores the content of the <script> tag.

Page 24: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

24

Components of JavaScript

Objects (hierarchy, dot syntax) Instance Properties Values Events and Event Handlers Variables Arrays Methods Operators (Assignment, Comparison, etc.) Functions

Example

Page 25: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

25

Types of variables

Numeric: 13, 22.3,-3.1415, 5.1E2 (5.1x102) String: any group of characters within

quotation marks. “This is a string”, ‘another string’, ‘3.14’, “” (the empty string)

Boolean: can only be true or falsevar isOk = true;

Page 26: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

26

Creating a Date object

JavaScript doesn’t have a Date data type. You need to create a date object that contains date information.

dateVariable = new Date(“month, day, year, hours:minutes:seconds”);

ordateVariable = new Date(year, month, day, hours,minutes,seconds);Examples:

• taxDay = new Date (“April, 15,2005,24:0:0”);or taxDay = new Date (2005,3,15,24,0,0);

• today= new Date ();

Page 27: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

27

Time in JavaScript

Seconds and minutes: 0-59 Hours:0-23 Week Days:0 (Sunday) – 6 (Saturday) Day: 1-31 (day of the month) Months:0 (January) –11 (December) Year: since 1900 Time: in millisecond since 6 p.m on 12/31/69

Example:

taxDay = new Date(2005,15,3,0,0,0);

Page 28: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

28

Date methods

For retrieving date and time value dateVariable.getFullYear(); dateVariable.getMonth(); dateVariable.getDate(); dateVariable.getDay(); dateVariable.getHours(); dateVariable.getMinutes(); dateVariable.getSeconds();

taxDay.getMonth(); //returns 3

taxDay = new Date (2005,15,3,0,0,0);

Example

ThisMonth = Today.getMonth()+1;

Page 29: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

29

Working with Expressions and Operators

Expressions are JavaScript commands that assign values to variables. Example: use the expression, daysLeft=999, to

assign the value 999 to the daysLeft variable

Expressions are created using variables, values, and operators.

One of the most commonly used operators is the + operator, which performs the action of adding or combining two elements. Example: use the plus operator in a program with

the following command:var ThisMonth = Today.getMonth()+1;

Page 30: 1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content

30

Addition and Concatenation

result = var1 + var2 If both are number variables:

Adds var1 to var2 Assigns the result to the variable result result is a number variable

If at least one of the two is a string: Concatenate var1 and var2 Assigns the result to the variable result result is a string variable