JavaScript for Interactive Web Pages

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Intro to Javascript CS Client Side Scripting CS380 2.
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
The Web Warrior Guide to Web Design Technologies
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript
Information Technology Center Hany Abdelwahab Computer Specialist.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
4.1 JavaScript Introduction
Scripting Languages.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
DOM and JavaScript Aryo Pinandito.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CSE 154 LECTURE 17: JAVASCRIPT. Client-side scripting client-side script: code runs in browser after page is sent back from server often this code manipulates.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
JavaScript Syntax, how to use it in a HTML document
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
CSE 154 Lecture 6: Javascript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Introduction to.
CGS 3066: Web Programming and Design Spring 2017
Build in Objects In JavaScript, almost "everything" is an object.
>> Introduction to JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Chapter 5 Scripting Language
Tutorial 10 Programming with JavaScript
Web Development & Design Foundations with HTML5 7th Edition
Chapter 5 Scripting Language
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
JavaScript an introduction.
Web Systems Development (CSC-215)
Objectives Insert a script element Write JavaScript comments
DHTML Javascript Internet Technology.
Your 1st Programming Assignment
WEB PROGRAMMING JavaScript.
DHTML Javascript Internet Technology.
PHP.
Tutorial 10 Programming with JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
JavaScript Intro.
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

JavaScript for Interactive Web Pages By Jinzhu Gao

Web Programming Step by Step, Chapter 7 Objectives Learn how to program web pages on the client side using JavaScript Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 JavaScript (7.1) A lightweight, dynamic scripting language used to implement dynamic client-side interfaces A web standard (but not supported identically by all browsers) NOT related to Java other than by name and some syntactic similarities Has ability to interact with the content of a web page and change that content in response to user actions insert dynamic text into HTML (ex: user name) react to events (ex: page load user click) get information about a user's computer (ex: browser type) perform calculations on user's computer (ex: form validation) Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 JavaScript vs. Java Interpreted, not compiled More relaxed syntax and rules key construct is the function rather than the class contained within a web page and integrates with its HTML/CSS content Web Programming Step by Step, Chapter 7

Client-Side Scripting Code runs in browser after page is sent back from server often this code manipulates the page or responds to user actions Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 JavaScript vs. PHP Similarities: both are interpreted, not compiled both are relaxed about syntax, rules, and types both are case-sensitive both have built-in regular expressions Differences: JS is less procedural, more object-oriented JS focuses on user interfaces and interacting with a document; PHP is geared toward HTML output and file/form processing JS code runs on the client's browser; PHP code runs on the web server Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 JavaScript vs. PHP PHP benefits: security: Has access to server's private data; client can't see source code compatibility: avoids browser compatibility issues power: fewer restrictions (can write to files, open connections to other servers, connect to databases, ...) JavaScript benefits: usability: can modify a page without having to post back to the server (faster UI) efficiency: can make small, quick changes to page without waiting for server event-driven: can respond to user actions like clicks and key presses Web Programming Step by Step, Chapter 7

Event-Driven Programming JavaScript programs wait for user actions called events and respond to them event-driven programming: writing programs driven by user events Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 A JavaScript Program Goal: make a page with a button that will cause some code to run when it is clicked. Steps: Creating the HTML control Write JavaScript event-handling function Create a JavaScript (.js) file. Attach event handler to control Attach the .js file to the .html page. event-driven programming: writing programs driven by user events Web Programming Step by Step, Chapter 7

Creating the HTML Control Buttons <button>: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd> <html xmlns=http://www.w3.org/1999/xhtml> <head><title>JavaScript example</title></head> <body> <div> <button>Say the Magic Word</button> </div> </body> </html> Say the Magic Word Web Programming Step by Step, Chapter 7

Write JavaScript Event-Handling Function Alert: cause the browser to show a pop-up message dialog box Web Programming Step by Step, Chapter 7

Attach Event Handler to Control script tag should be placed in HTML page's head script code is stored in a separate .js file Event attributes: onclick, onkeydown, onchange, onsubmit JS code can be placed directly in the HTML file's body or head (bad style) Web Programming Step by Step, Chapter 7

Attach Event Handler to Control // please.js function sayMagicWord() { alert(“PLEASE!”); } <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd> <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>JavaScript example</title> <script src=“please.js” type=“text/javascript”></script> </head> <body> <div> <button onclick=“sayMagicWord();”>Say the Magic Word</button> </div> </body> </html> Say the Magic Word Web Programming Step by Step, Chapter 7

Document Object Model (DOM) A set of JavaScript objects that represent the content of the current HTML page most JS code manipulates elements on an HTML page we can examine the state of the elements, e.g. whether a box is checked we can change state, e.g. putting text into a div we can change styles, e.g. make a paragraph red Web Programming Step by Step, Chapter 7

Accessing DOM Objects by ID <button onclick=“sayMagicWord();”>Say the Magic Word</button>\ <p id=“result”>What do you say?</p> What do you say? Say the Magic Word function sayMagicWord() { var paragraph = document.getElementById(“result”); paragraph.innerHTML = “PLEASE!”; } PLEASE! The code does not permanently modify the web page. innerHTML: represents the text or HTML content inside an element Say the Magic Word Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Self-Check Page 283 Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 JavaScript Syntax (7.2) Variables and Types variables are declared with the var keyword (case sensitive) types are not specified, but JS does have types ("loosely typed") Number, Boolean, String, Array, Object, function, null, undefined can find out a variable's type by calling typeof Type Description Examples Number Integer or real number 42, -17, 3.14, 2.4e-6 Boolean Logical value True, false String Text string “Hello there”, ‘how are you’ Array Indexed list of elements [12, 17, -5] Object Entity containing data and behavior {name: “Marty”, age: 12} function Group of statements to execute function message() { alert(“Hello, world!”); } null An empty value undefined The lack of a value; an undefined value Variables are loosely typed Variables don’t need to be explicitly declared before they are used Web Programming Step by Step, Chapter 7

JavaScript Syntax - Numbers and Arithmetic integers and real numbers are the same type (no int vs. double) same operators: + - * / % ++ -- = += -= *= /= %= similar precedence to Java many operators auto-convert types: "2" * 3 is 6 Web Programming Step by Step, Chapter 7

JavaScript Syntax - Comments identical to Java's comment syntax recall: 4 comment syntaxes HTML: <!-- comment --> CSS/JS/PHP: /* multi-line comment */ Java/JS/PHP: // single-line comment PHP: # comment Web Programming Step by Step, Chapter 7

JavaScript Syntax – Strings methods: charAt, charCodeAt, fromCharCode, indexOf, lastIndexOf, replace, split, substring, toLowerCase, toUpperCase charAt returns a one-letter String (there is no char type) length property (not a method as in Java) Strings can be specified with "" or '' concatenation with + : 1 + 1 is 2, but "1" + 1 is "11" escape sequences behave as in Java: \' \" \& \n \t \\ Web Programming Step by Step, Chapter 7

JavaScript Syntax – for loop Web Programming Step by Step, Chapter 7

JavaScript Syntax – arguments array Web Programming Step by Step, Chapter 7

JavaScript Syntax – math Object methods: abs, ceil, cos, floor, log, max, min, pow, random, round, sin, sqrt, tan properties: E, PI Web Programming Step by Step, Chapter 7

JavaScript Syntax – null and undefined undefined : has not been declared, does not exist null : exists, but was specifically assigned a null value Web Programming Step by Step, Chapter 7

JavaScript Syntax – Using DOM Objects tagName: element's HTML tag, capitalized; div.tagName is "DIV" className: CSS classes of element; div.className is "foo bar" innerHTML: HTML content inside element; div.innerHTML is "\n  <p>Hello, <em>very</em> happy to ... src: URL target of an image; image.src is "images/borat.jpg" Web Programming Step by Step, Chapter 7

JavaScript Syntax – Using DOM Objects value: the text in an input control sid.value could be "1234567" checked, disabled, readOnly: whether a control is selected/disabled/etc. frosh.checked is true Web Programming Step by Step, Chapter 7

JavaScript Syntax – Using DOM Objects multiply.js Web Programming Step by Step, Chapter 7

Document Object Model (DOM) For more info on DOM Objects, please read Chapter 9 http://www.webstepbook.com/supplements-2ed/slides/chapter09- dom.shtml Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Self-Check Page 305 Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Program Logic (7.3) Logic Operators > < >= <= && || ! == != === !== most logical operators automatically convert types: 5 < "7" is true 42 == 42.0 is true "5.0" == 5 is true === and !== are strict equality tests; checks both type and value "5.0" === 5 is false Web Programming Step by Step, Chapter 7

Program Logic – Boolean Values Web Programming Step by Step, Chapter 7

Program Logic – if/else Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Program Logic – while Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Scope, global and local variables Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Function parameters/return Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Calling Functions Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Common Bug: Spelling Error Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Arrays Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Popup Boxes Web Programming Step by Step, Chapter 7

Advanced JavaScript Syntax (7.4) Date object Web Programming Step by Step, Chapter 7

Debugging Common Errors JavaScript's syntax is looser than Java's, but its errors are meaner Web Programming Step by Step, Chapter 7

Debugging JS Errors using Firebug Web Programming Step by Step, Chapter 7

Finding JS Errors using JSLint Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Debugging Checklist Are you sure the browser is even loading your JS file at all? Put an alert at the top of it and make sure it appears. When you change your code, do a full browser refresh (Shift-Ctrl-R) Check bottom-right corner of Firefox for Firebug syntax errors. Paste your code into our JSLint tool to find problems. Type some test code into Firebug's console or use a breakpoint. Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Debugging Checklist “My program does nothing” Is the browser even loading my script file? If so, is it reaching the part of the file that I want it to reach? If so, what is it doing once it gets there? Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Debugging Checklist Is my JS file loading? Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Debugging Checklist Is it reaching the code I want it to run? Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Debugging Checklist Object 'foo' has no properties Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Debugging Checklist Common bug: bracket mismatches Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Self-Check Page 321 Reading Assignment: Ch 8.5 Case Study: Hangman http://www.pacificescience.com/jgao/COMP127/code/ch08- javascript/hangman5.html Web Programming Step by Step, Chapter 7

Web Programming Step by Step, Chapter 7 Summary JavaScript vs. Java vs. PHP JavaScript Syntax Web Programming Step by Step, Chapter 7