60
CSC309 Winter 2016 Lecture 3 Larry Zhang 1

CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

CSC309 Winter 2016 Lecture 3Larry Zhang

1

Page 2: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Why Javascript• Javascript is for dynamically manipulate the front-end of your web

page.

• Add/remove/change the content/attributes of an HTML element

• Change the style of an HTML element

• Detect user interactions with the web page.

• …, pretty much everything with the front-end

2

Page 3: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

How it works• it runs only on client side (the browser), i.e., it does not communicate with the

server side (normally)

• So Javascript can do fast UI

• It is interpreted language, interpreted by a Javascript Engine built in the browser.

• V8 (Chrome), SpiderMonkey (Firefox), JavascriptCore (Safari)

• It is event-driven, i.e., all actions are triggered by a user event

• e.g., user clicking on something, or page loaded

• It has nothing really to do with Java

3

Page 4: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The Iron Triangle

• HTML: defines the content of the web page

• CSS: specify the style of the web page

• Javascript: program the behaviour of the web page.

4

Page 5: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

JS programming model: Event-Driven• There is no “main” function

• Every function call is trigger by an event happening at the web page.

• If no event happens, the javascript are just like a static chunk of code and nothing happens.

5

Page 6: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Before we start programming Javascript, we need to know the environment that is runs in.

We know javascript can manipulate the HTML document, but how does JS get access to the elements in the HTML document?

6

Page 7: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Document Object Model (DOM)• It is a cross-platform language-independent

convention for representing and interacting with objects in HTML and XML document.

• Standard set by W3C.

• It is how the browser internally organized the objects in a loaded HTML document.

• It is the programming interface for HTML.

• The objects form a DOM tree.

7

Page 8: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Document Object Model (DOM)

Every element is an object.

8

Page 9: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Find an element and change it• Find an HTML element

• document.getElementById(id)

• document.getElementsByTagName(name)

• document.getElementsByClassName(name)

• Change HTML elements

• element.innerHTML = new html content

• element.attribute = new value

• element.setAttribute(attribute, value)

• element.style.property = new style

demo

9

Page 10: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Event handler: onclick<!DOCTYPE html> <html> <body>

<h1>My First Page</h1>

<p id="demo"></p>

<script> document.getElementById("demo").innerHTML = "Hello World!"; </script>

</body> </html>

This action is trigger when the script is loaded. What if I want the action to be triggered by a click on something?

Use the “onclick” handler.

demo

10

Page 11: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Define a function<!DOCTYPE html> <html> <body>

<h1>My First Page</h1>

<p id="demo">paragraph</p>

<button onclick='document.getElementById("demo").innerHTML = "HAHA"'>I'm a button</button>

<script> </script>

</body> </html>

This is ugly, too much stuff in the line

demo

11

Page 12: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Decoupling HTML and Javascript<!DOCTYPE html> <html> <body>

<h1>My First Page</h1>

<p id="demo">paragraph</p>

<button onclick=“myfun();”>I’m a button</button>

<script> function myfun () { document.getElementById("demo").innerHTML = "HAHA"; } </script>

</body> </html> This is still ugly. Shouldn’t the content definition (HTML) and the

behaviour definition (Javascript) be completely decoupled?

demo

12

Page 13: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The good code<!DOCTYPE html> <html> <body>

<h1>My First Page</h1>

<p id="demo">paragraph</p>

<button id="button1">I'm a button</button>

<script> document.getElementById("button1").onclick = function () { document.getElementById("demo").innerHTML = "HAHA"; } </script>

</body> </html> This part can now be put into a separate file.

13

Page 14: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Separate files<!DOCTYPE html> <html> <body>

<h1>My First Page</h1>

<p id="demo">paragraph</p>

<button id="button1">I'm a button</button>

<script src="myScript.js"></script>

</body> </html>

// myScript.js

document.getElementById("button1").onclick = function () { document.getElementById("demo").innerHTML = "HAHA"; }

• Performance tip: it is typically a good idea to put the scripts at the bottom of the body, it can improve page load, since loading and compiling Javascripts can take time.

• Putting Javascript in a separate file also improve page load, since the script file can be cached.

14

Page 15: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Note

• In CSS we have font-size, background-color

• In JS when setting style we use fontSize, backgroundColor

15

Page 16: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Anonymous function

16

document.getElementById("button1").onclick = function () { document.getElementById("demo").innerHTML = "HAHA"; }

document.getElementById("button1").onclick = normalFunc;

function normalFunc () { document.getElementById("demo").innerHTML = "HAHA"; }

Named function

Page 17: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Readings

• More on DOM

• http://www.w3schools.com/js/js_htmldom.asp

• More on Events

• http://www.w3schools.com/js/js_events.asp

17

Page 18: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

We’ve got an idea how to write Javascript. Now let’s learn the language a bit more systematically.

18

Page 19: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Variables

19

Page 20: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Number types

20

Page 21: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Math object

21

Page 22: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Special values: null and undefined

22

Page 23: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Logical operators

23

Page 24: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

if/else statements

24

Page 25: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Boolean types

25

Page 26: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

for loop

26

Page 27: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

while loop

27

Page 28: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Pop-up

28

Page 29: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Arrays

29

Page 30: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Array methods

30

Page 31: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

String type

31

Page 32: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

More about strings

32

Page 33: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Split and join

33

Page 34: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Reference: http://www.w3schools.com/js/default.asp

34

Page 35: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Browser Object Hierarchy

35

Page 36: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

36

Page 37: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Global DOM

37

Page 38: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The window object

38

Page 39: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The document object

39

Page 40: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The location object

40

Page 41: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The navigator object

41

Page 42: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The screen object

42

Page 43: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The history object

43

Page 44: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The DOM Tree

44

Page 45: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The DOM Tree

45

Page 46: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Types of DOM nodes

46

Page 47: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Types of DOM nodes

47

Page 48: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Traversing the DOM tree

48

Full list: https://developer.mozilla.org/en-US/docs/Web/API/Node

Page 49: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Create new nodes

49

Page 50: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Modifying the DOM tree

50

Page 51: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

Which way is better?

51

VS

Better

• Hack • Ugly, if the added element

is complicated. • error-prone, especially with

a mixed ‘’ and “” • can only add the end,

cannot insert in the middle

Page 52: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

When does the code run?

52

Page 53: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

When does the code run?

53

Page 54: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

a failing example

54

• Problem: global JS code runs the moment the script is loaded • Script in head is processed before the page body is loaded • No element with id=“ok” has been created yet.

Page 55: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

a solution: window.onload

55

Page 56: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The keyword this

56

Page 57: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

The keyword this

57

• The event handler okayClick is bound to the element with id “ok” • So this means the element with id “ok” • No need to do getElementById() again.

Page 58: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

• We’ve only covered a small part of things you can do with Javascript

• You should go through the reference to get a better sense what are all the things

• http://www.w3schools.com/js/default.asp

• When you need to do it, look it up exact how to

• Use the Developer Console

58

Page 59: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

demo: making a game http://www.cs.toronto.edu/~ylzhang/csc309w16/puzzle/game.html

59

Page 60: CSC309 Winter 2016 Lecture 3 › ~ylzhang › csc309w16 › files › lec03... · 2016-01-19 · CSC309 Winter 2016 Lecture 3 Larry Zhang 1. Why Javascript ... The navigator object

• Today we learned

• Javascript

• Next week

• JQuery, Ajax; PHP

60