introduction to the www...history •need for client side interaction –code executes on the local...

47
JavaScript CMPT 281

Upload: others

Post on 16-Aug-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript

CMPT 281

Page 2: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Outline

• Introduction to JavaScript

• Resources

• What is JavaScript?

• JavaScript in web pages

Page 3: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Announcements

Page 4: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Assignment 3

• Layout with tables

Page 5: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Resources

Page 6: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Resources

Page 7: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Why JavaScript?

Page 8: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

HTML + CSS + JavaScript

Page 9: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Separation of Concerns

Page 10: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Separation of Concerns

Page 11: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

What is Javascript?

Page 12: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

What is Javascript?

• A scripting programming language

– Cannot be run without a browser

– Embedded in most web browsers

• A way to create web pages that respond dynamically to user action

• A tool for server-side development

Page 13: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Types of computer languages

• High-Level vs. Low Level

• Compiled vs. Interpreted

• Structured vs. Object Oriented

• Scripting vs. Programming

Page 14: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript

• High-Level vs. Low Level

• Compiled vs. Interpreted

• Structured vs. Object Oriented

• Scripting vs. Programming

Page 15: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

History

• Need for ‘client side’ interaction

– Code executes on the local machine

– No need for network connection once script runs

• Developed by Netscape in 1995

– (Not related to the Java Language)

• Actually ECMAScript (ECMA-262)

– en.wikipedia.org/wiki/ECMAScript

– Several variants (JScript, Action Script…)

Page 16: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

What can JavaScript do?

• It is a full programming language

– API (application programming interface) is specific to working with browsers

• Restrictions:

– Security-based limitations

• No networking

• No access to local file system

– Limited UI toolkit and graphics

• (This is changing with HTML5)

Page 17: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

What can JavaScript do?

• Benefits:

– Close integration with the browser

• Access the webpage and all elements within

• Adjust or create HTML

• Open and resize browser windows

• Run animations, play sounds

Page 18: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Example: writing to the page

Page 19: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Example: loading a random image

Page 20: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Where do scripts go?

• In the HMTL page

• Like styles, can be external, internal, or inline

– Use these for different situations

Page 21: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Where do scripts go?

• For scripts that respond to user events, and for functions:

– Either external or internal

– In <head>…</head>

• For scripts that write document content:

– In <body>…</body>

Page 22: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Body example

<html>

<head>

</head>

<body>

<script type="text/javascript">

document.write("This message written by JavaScript");

</script>

</body>

</html>

Page 23: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Internal example

<html> <head> <script type="text/javascript"> function message() { alert("This alert was called with the onload event"); } </script> </head> <body onload="message()"> </body> </html

Page 24: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

External example

<html>

<head>

<script type="text/javascript" src="xyz.js"></script>

</head>

<body>

</body>

</html>

Page 25: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

How does JS access the page?

• DOM: Document Object Model

– A framework to describe a web page (document) in a tree-like structure

Page 26: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

DOM

Page 27: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

DOM

Page 28: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Using the DOM to get at the page

<html> <body> <p id="intro">Hello World!</p> <script type="text/javascript"> x=document.getElementById("intro"); document.write(x.firstChild.nodeValue); </script> </body> </html>

Page 29: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Using the DOM to get at the page

<html> <body> <p id="intro">Hello World!</p> <script type="text/javascript"> x=document.getElementById("intro"); document.write(x.firstChild.nodeValue); </script> </body> </html>

Page 30: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Basics

Page 31: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Basics

• Statements

• Variables

• Events

• Functions

• Dialogs

Page 32: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Statements

<script type="text/javascript">

var a = 10;

var b = 11;

var c;

c = a + b;

alert(‘The answer is’ + c);

</script>

Page 33: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Variables

• Variables are used to store data.

• Rules for variable names:

– Variable names are case sensitive

– They must begin with a letter or the underscore character

• strname – STRNAME (not same)

Page 34: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Variables

• Variables: containers for data

– All variables have

• Name

• Type – JavaScript is loosely typed

• Value or “null”

• To declare a variable – var variableName

• Beware of reserved words: – E.g., ‘if’, ‘Document’, ‘Math’, etc.

Page 35: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Operators

• Arithmetic Operators

Operator Description Example Result

+ Addition x=2 4

y=2

x+y

- Subtraction x=5 3

y=2

x-y

* Multiplication x=5 20

y=4

x*y

/ Division 15/5 3

5/2 2,5

% Modulus (division remainder)

5%2 1

10%8 2

10%2 0

++ Increment x=5 x=6

x++

-- Decrement x=5 x=4

x--

Page 36: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Operators

• Assignment Operators

Operator Example Is The Same As

= x=y x=y

+= x+=y x=x+y

-= x-=y x=x-y

*= x*=y x=x*y

/= x/=y x=x/y

%= x%=y x=x%y

Page 37: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Operators

• Comparison Operators

Operator Description Example

== is equal to 5==8 returns false

=== is equal to (checks for both value and type)

x=5

y="5"

x==y returns true

x===y returns false

!= is not equal 5!=8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to

5>=8 returns false

<= is less than or equal to 5<=8 returns true

Page 38: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Operators

• Logical Operators

Operator Description Example

&& and x=6

y=3

(x < 10 && y > 1) returns true

|| or x=6

y=3

(x==5 || y==5) returns false

! not x=6

y=3

!(x==y) returns true

Page 39: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

User and Browser Events

Page 40: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Functions

<script type=“text/javascript”>

function dontClick()

{

alert("I told you not to click!");

}

</script>

...

<input type="button" value="Don't Click Me!" onClick="dontClick()"/>

Page 41: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Dialogs

• alert("Hello!");

• confirm("Delete files?");

• var name=prompt("Your name?", "Tim Berners-Lee");

Page 42: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Coding Tips

• Check your statements are on one line

• Check your " and ' quotes match

• Take care with capitalisation

• Lay it out neatly - use tabs

• Use the Chrome developer tools

– Or Firebug for Firefox

Page 43: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

A Word About Comments

• JavaScript Comments – These comments must be within a script – // begins a single line comment

• These can start anywhere on the line, but the remainder of the line becomes a comment

– /* All of your comments here… */ • Everything between the start and end comment markers are

comments • Can be on a single line or span many…

• HTML Comments – <!-- All of your comments here… -->

Page 44: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Ending Statements With a Semicolon?

• With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon (;).

• Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional!

• Semicolons are required if you want to put more than one statement on a single line.

Page 45: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Example

Page 46: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

JavaScript Libraries

Prototype www.prototypejs.org/

script.aculo.us script.aculo.us/

Yahoo! User Interface Library developer.yahoo.com/yui/

Dojo dojotoolkit.org/

jQuery jquery.com/

MooTools mootools.net/

Page 47: Introduction to the WWW...History •Need for client side interaction –Code executes on the local machine –No need for network connection once script runs •Developed by Netscape

Reminders for this week

• Do the JavaScript tutorials on w3schools:

– 'JS Basic‘: www.w3schools.com/js/

• Assignment

• Reading:

– Pattern group I: Designing Effective Page Layouts

• Pages 631-657