cs50 week 9 sam green ’17 [email protected] 646-457-2340

11
CS50 Week 9 Sam Green ’17 [email protected] 646-457-2340

Upload: domenic-bryan

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

CS50 Week 9

Sam Green ’[email protected]

646-457-2340

Page 2: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Today’s Agenda

JavaScript: Basics & Use CasesDOM – Document Object Model

Practice with JS and the DOMA Toy API Example

PSet

Page 3: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

What is JavaScript?

Client-side dynamic programming language Speed and easy of use from client side language.

JS doesn’t fit into one category of programmingHas elements of functional, object-oriented, and imperative programming

Interpreted, not compiled (like PHP)Can manipulate the DOM to change HTML – one of the reasons it is so popular.

Page 4: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Functions as Values

Functions in JavaScript are values.They can be assigned to variables.

They can be passed as arguments or returned as results from other function calls.

Page 5: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Anonymous Functions

var f = function(){

alert(“Sam!!!!”);};f();

Page 6: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Objects & Functions as Values

An object is a container for related values.Some of these values can be functions (!).

Example: JS onsubmit function from lecture.Example: JS onclick

Page 7: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Document Object Model (DOM)

Hierarchical way of laying out information HTML webpage is an example.Let’s draw a DOM tree for

<!DOCTYPE html> <head> <title id=“title”>Hey Sam</title> </head> <body> <div> <img id = “pic src=“rob.jpg” alt = “rob> </div> </body></html>

Page 8: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

JS and the DOM

JS allows us to dynamically alter the DOM, without reloading the page.

Key point on this: we can use JS to select elements from the DOM and then change them.

Page 9: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

JS Validation

What are the benefits of client-side form validation?

What can we validate?Is the PHP back-end still necessary?

Page 10: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Let’s do some DOM manipulation examples.

Page 11: CS50 Week 9 Sam Green ’17 samuelgreen@college.harvard.edu 646-457-2340

Ajax and Asynchronous Requests

Ajax is an JavaScript paradigm for asynchronous web requests.

Ajax is let’s webapps be powerful.Example in life: when new tweets appear on

your Twitter feed, that happens via Ajax.