CSC309 Winter 2016 Lecture 3. Larry Zhang

Similar documents
Example. Represent this as XML

MASTERTAG DEVELOPER GUIDE

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Technical University of Sofia Faculty of Computer Systems and Control. Web Programming. Lecture 4 JavaScript

JavaScript Basics & HTML DOM. Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com

Mobile Web Applications. Gary Dubuque IT Research Architect Department of Revenue

Overview. In the beginning. Issues with Client Side Scripting What is JavaScript? Syntax and the Document Object Model Moving forward with JavaScript

Web Programming Step by Step

Visualizing an OrientDB Graph Database with KeyLines

DIPLOMA IN WEBDEVELOPMENT

Visualizing a Neo4j Graph Database with KeyLines

Lecture 9 Chrome Extensions

Short notes on webpage programming languages

«W3Schools Home Next Chapter» JavaScript is THE scripting language of the Web.

The purpose of jquery is to make it much easier to use JavaScript on your website.

Instructions for Embedding a Kudos Display within Your Website

Yandex.Widgets Quick start

AJAX The Future of Web Development?

PLAYER DEVELOPER GUIDE

CarTrawler AJAX Booking Engine Version: 5.10 Date: 01/10/13.

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Client-side programming with JavaScript. Laura Farinetti Dipartimento di Automatica e Informatica Politecnico di Torino laura.farinetti@polito.

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

Client-side Web Engineering From HTML to AJAX

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

CMSC434 TUTORIAL #3 HTML CSS JavaScript Jquery Ajax + Google AppEngine Mobile WebApp HTML5

GUI and Web Programming

Web Design Technology

Advantage of Jquery: T his file is downloaded from

Online Multimedia Winter semester 2015/16

jquery Tutorial for Beginners: Nothing But the Goods

JavaScript: Client-Side Scripting. Chapter 6

Rich Internet Applications

Progressive Enhancement With GQuery and GWT. Ray Cromwell

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

LAB MANUAL CS (22): Web Technology

jquery Programming Cookbook jquery Programming Cookbook

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Certified PHP/MySQL Web Developer Course

CIS 467/602-01: Data Visualization

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

Web. Programming. Hans- Pe0er Halvorsen, M.Sc. h0p://home.hit.no/~hansha/?page=sojware_development

ANGULAR JS SOFTWARE ENGINEERING FOR WEB SERVICES HASAN FAIZAL K APRIL,2014

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 06 (Nebenfach)

Your First Web Page. It all starts with an idea. Create an Azure Web App

Apple Applications > Safari

Technical University of Sofia Faculty of Computer Systems and Control. Web Programming. Lecture 5 JavaScript part 2

Web Designing with UI Designing

the intro for RPG programmers Making mobile app development easier... of KrengelTech by Aaron Bartell

Web Building Blocks. Joseph Gilbert User Experience Web Developer University of Virginia Library

A little about me: I m a Computer Science Engineer I m Spanish but living in Finland. I ve been working and contributing to Drupal for more than 3

Abusing HTML5. DEF CON 19 Ming Chow Lecturer, Department of Computer Science TuCs University Medford, MA

opalang - Rapid & Secure Web Development

Rich User Interfaces for Web-Based Corporate Applications

This matches a date in the MM/DD/YYYY format in the years The date must include leading zeros.

Hello World RESTful web service tutorial

WIRIS quizzes web services Getting started with PHP and Java

Web Application Development

Write a Web Application for Free Edition 2

Introduction to web development and JavaScript

Web Development 1 A4 Project Description Web Architecture

IMRG Peermap API Documentation V 5.0

AJAX and JSON Lessons Learned. Jim Riecken, Senior Software Engineer, Blackboard Inc.

Interactive Data Visualization for the Web Scott Murray

OIT 307/ OIT 218: Web Programming

SUBJECT CODE : 4074 PERIODS/WEEK : 4 PERIODS/ SEMESTER : 72 CREDIT : 4 TIME SCHEDULE UNIT TOPIC PERIODS 1. INTERNET FUNDAMENTALS & HTML Test 1

INTERNET PROGRAMMING AND DEVELOPMENT AEC LEA.BN Course Descriptions & Outcome Competency

The Google Web Toolkit (GWT): Declarative Layout with UiBinder Basics

Debugging JavaScript and CSS Using Firebug. Harman Goei CSCI 571 1/27/13

Honoring a Tradition of Simplicity. The Foundation. Getting Started

Website Login Integration

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

A Model of the Operation of The Model-View- Controller Pattern in a Rails-Based Web Server

How to Code With MooTools

Developing Offline Web Application

GLEN RIDGE PUBLIC SCHOOLS MATHEMATICS MISSION STATEMENT AND GOALS

Overview. Understanding Web Design. Big Ideas. Goals & Audience. Theme. Theme. Big ideas. Goals & Audience Theme Navigation

A send-a-friend application with ASP Smart Mailer

COURSE SYLLABUS EDG 6931: Designing Integrated Media Environments 2 Educational Technology Program University of Florida

ACCESSING THE PROGRESS OPENEDGE APPSERVER FROM PROGRESS ROLLBASE USING JSDO CODE

It is highly recommended that you are familiar with HTML and JavaScript before attempting this tutorial.

HTML5 Data Visualization and Manipulation Tool Colorado School of Mines Field Session Summer 2013

Developing ASP.NET MVC 4 Web Applications Course 20486A; 5 Days, Instructor-led

Ajax Performance Tuning and Best Practice

DESIGNING HTML HELPERS TO OPTIMIZE WEB APPLICATION DEVELOPMENT

Ajax Development with ASP.NET 2.0

Developing ASP.NET MVC 4 Web Applications MOC 20486

Learn. JavaScript. Steve Suehring

Programming in HTML5 with JavaScript and CSS3

Step by step guides. Deploying your first web app to your FREE Azure Subscription with Visual Studio 2015

Finding XSS in Real World

Website Implementation

Building Ajax Applications with GT.M and EWD. Rob Tweed M/Gateway Developments Ltd

Three Approaches to Web. with RPG

Client-side Development using HTML, Javascript and CSS

Chapter 1 Programming Languages for Web Applications

Web Development using PHP (WD_PHP) Duration 1.5 months

1. User Guide API overview addon - xml Definition addon.background addon.background.script

JavaScript: Arrays Pearson Education, Inc. All rights reserved.

HTML5. Turn this page to see Quick Guide of CTTC

Transcription:

CSC309 Winter 2016 Lecture 3 Larry Zhang 1

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

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

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

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

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

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

Document Object Model (DOM) Every element is an object. 8

Find an element and change it Find an HTML element document.getelementbyid(id) document.getelementsbytagname(name) document.getelementsbyclassname(name) Change HTML elements demo element.innerhtml = new html content element.attribute = new value element.setattribute(attribute, value) element.style.property = new style 9

Event handler: onclick <!DOCTYPE html> <html> <body> demo <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. 10

Define a function demo <!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 11

Decoupling HTML and Javascript <!DOCTYPE html> <html> <body> demo <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? 12

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

Separate files <!DOCTYPE html> <html> <body> <h1>my First Page</h1> // myscript.js document.getelementbyid("button1").onclick = function () { document.getelementbyid("demo").innerhtml = "HAHA"; } <p id="demo">paragraph</p> <button id="button1">i'm a button</button> <script src="myscript.js"></script> </body> </html> 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

Note In CSS we have font-size, background-color In JS when setting style we use fontsize, backgroundcolor 15

Anonymous function document.getelementbyid("button1").onclick = function () { document.getelementbyid("demo").innerhtml = "HAHA"; } Named function document.getelementbyid("button1").onclick = normalfunc; function normalfunc () { document.getelementbyid("demo").innerhtml = "HAHA"; } 16

Readings More on DOM http://www.w3schools.com/js/js_htmldom.asp More on Events http://www.w3schools.com/js/js_events.asp 17

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

Variables 19

Number types 20

Math object 21

Special values: null and undefined 22

Logical operators 23

if/else statements 24

Boolean types 25

for loop 26

while loop 27

Pop-up 28

Arrays 29

Array methods 30

String type 31

More about strings 32

Split and join 33

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

Browser Object Hierarchy 35

36

Global DOM 37

The window object 38

The document object 39

The location object 40

The navigator object 41

The screen object 42

The history object 43

The DOM Tree 44

The DOM Tree 45

Types of DOM nodes 46

Types of DOM nodes 47

Traversing the DOM tree Full list: https://developer.mozilla.org/en-us/docs/web/api/node 48

Create new nodes 49

Modifying the DOM tree 50

Which way is better? Better VS 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 51

When does the code run? 52

When does the code run? 53

a failing example 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. 54

a solution: window.onload 55

The keyword this 56

The keyword this 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. 57

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

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

Today we learned Javascript Next week JQuery, Ajax; PHP 60