JavaScript I ECT 270 Robin Burke.

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

Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
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
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Tutorial 10 Programming with JavaScript
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to scripting
4.1 JavaScript Introduction
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Scripting Languages.
Programming with JavaScript (Chapter 10). XP Various things Midterm grades: Friday Winter Career Fair – Thursday, April 28, 2011 (11 am to 3 pm). – MAC.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
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.
1 JavaScript in Context. Server-Side Programming.
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. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript I ECT 270 Robin Burke. Outline Midterm answers JavaScript Programming Syntax Interacting with the browser Variables and data types Arrays Flow.
Client-side processing in JavaScript.... JavaScript history Motivations –lack of “dynamic content” on web pages animations etc user-customised displays.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
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.
JavaScript, Fourth Edition
1 JavaScript in Context. Server-Side Programming.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to.
Module 1 Introduction to JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Tutorial 10 Programming with JavaScript
JavaScript is a programming language designed for Web pages.
JavaScript Syntax and Semantics
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
14 A Brief Look at JavaScript and jQuery.
JavaScript Introduction
DHTML Javascript Internet Technology.
Introduction to JavaScript for Python Programmers
DHTML Javascript Internet Technology.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
Introduction to JavaScript
Tutorial 10 Programming with JavaScript
JavaScript What is JavaScript? What can JavaScript do?
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
An Introduction to JavaScript
JavaScript: Introduction to Scripting
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

JavaScript I ECT 270 Robin Burke

Outline JavaScript Homework #6 Programming Syntax Interacting with the browser Variables and data types Arrays Flow of control Function definitions Date object Homework #6

Why JavaScript? Web application round-trip expensive Web pages static no way to do computation on the client side example: form validation Web pages static no way to allow users to interact with the page example: popup link menus What is needed client-side code

JavaScript Very little connection to Java JavaScript is Dynamic HTML marketing move by Netscape JavaScript is a scripting language for web clients interpreted untyped Dynamic HTML combination of JavaScript, CSS and DOM to create very flexible web page presentation

JavaScript history Introduced with Netscape 2.0 MS copied with JScript In 1998, ECMAScript became a standard proprietary names still used ECMA sets minimum standards

JavaScript Programming Easy to work with edit page view in browser no compilation / linking / libraries / etc Easy to get in trouble easy to skip documentation, proper coding practices Headaches browser compatibility browser-version compatibility

Best Practices Naming Documentation Style Embedded output lower/upper for variable names e.g. "upperRight". Documentation Documentation of each significant variable Sparse documentation within the body of a function (usually on the right side of expressions) so as not to interfere with understanding the flow of control Style Careful delineation of the start and end of functions Alignment of expressions so that differences between similar expressions can easily be spotted Embedded output Format HTML so it closely corresponds with the actual appearance of the output Goal = Understandable code

Our Practice: PDL Write PDL first as comments program design language what the program should do in "application" terms, not code terms no variable names no operators Integrate PDL with code as it is written

Example PDL add the right margin to the block offset to find the horizontal position of the block Program h = marginR + offset; find the highest and lowest scores on exam ...

Our Practice: Iterative Development Working ≠ Finished When the program works, ask how could it be improved? what are the vectors of change? Dimensions of improvement readability / organization compactness cleanliness modularity / flexibility efficiency

Our Practice: In-class coding Programming is an interactive activity hard to teach in lecture setting We'll program our examples together

Debugging JavaScript Browser settings Internet Options.../Advanced Display a notification about every script error Disable script debugging

JavaScript and HTML Identifying script sections <script> and </script> But some browser can't / don't process Problem script becomes part of the page content Solution enclose script in HTML comments

JavaScript skeleton <html> <head> ... HTML head content ... <script language="JavaScript" type="text/javascript"> <!— ... code here executed on loading ... //--> </script> </head> <body> ... page content ... <!-- ... code here executed during page rendering ... ... more page content ... </body> </html>

JavaScript execution model Interpreted Code is executed as read Outside of SCRIPT elements HTML output as usual Inside SCRIPT elements JavaScript evaluated May or may not result in output to the page At the end of the page JavaScript program terminates but code may still be resident event handling

Basic syntax Familiar from Java Statement end  ; a = a + 1; Grouping statements  { } if foo { bar; } Comments  // and /* */ // this is a comment Case-sensitive

Objects JavaScript uses objects Methods Fields dot syntax to access fields and methods Methods document.write ("foo"); calls the method write on object document with argument "foo" Fields document.lastModified gets the date from the HTTP header

Interacting with the browser Built-in objects window methods and fields related to the browser document methods and fields related to a particular document displayed in the browser We can manipulate the browser and its contents programmatically dynamic HTML

window window.navigator window.frames[] window.status window.history an object describing the user's browser useful for code that depends on a particular browser version example: window.navigator.appName window.frames[] an array containing all of the frames in a framed document example: window.frame[0] window.status access to the contents of the status bar in the browser example: window.status window.history access to the browser's history list example: window.history.back()

window, cont'd opening new windows window.open window.alert() creates a new browser window (pop-up) example: window.open ("www.cti.depaul.edu") window.alert() opens a dialog with a message example: window.alert ("The system has crashed.") window.confirm() opens a dialog that the user can OK or cancel returns true if OK example: window.confirm ("Proceed to erase hard disk?") window.prompt() opens a dialog that returns a value example:window.prompt("Enter user id:")

document document.images document.write / writeln document.forms all the images in the document example: document.images[2] document.write / writeln methods for adding content to the document example: document.writeln ("foo"); document.forms all the forms in the document document.all all the HTML elements in the document

Elements We have access to the HTML elements in the document change attributes change contents

Examples Hello, world Browser info Pop-up write text to the page write HTML to the page Browser info write browser appName and version to the page Pop-up use the alert and open methods

Variables and data types JavaScript data types numeric string boolean object reference like in Java ignore book viz. "null" Remember JavaScript is untyped no type declarations you have to keep track of what is stored where

Declaration Variables do not have to be declared But, for this class using a variable makes it exist But, for this class all variables must be declared unlike Java, no types Declaration var foo;

Arrays Normally JavaScript doesn't require Example A fixed-size collection of identically typed data items distinguished by their indices JavaScript doesn't require fixed size identical types Example document.images[0]

Arrays cont'd Creating an array Accessing Legal but a bad idea var a = new Array (); Accessing a[0] = 5; a[1] = "foo"; a[2] = 10; b = a[0] + a[2]; Legal but a bad idea

Example Image swap Image swap, with preloading Multi image swap wait for alert, then swap images Image swap, with preloading Multi image swap

Control flow Standard options available Conditional Loops for while do

If statement The same as Java if test { ... if part ... } else { ... else part ...

Example Conditional image display display second image if user answers "OK" to a confirmation

For loop Mostly the same as Java Variant for object properties for (i = 0; i < 5; i++) { ... repeated part ... } Variant for object properties for (i in documents)

Example Slide show once-through recycling

Functions In Java In JavaScript you wrote functions associated with classes methods In JavaScript we won't be defining new classes we write functions with global scope can be called anywhere on the page next week we'll see how functions can be called from events functions can also simplify programs

Functions cont'd Syntax Placement best place is in the HEAD element function name(parameter list) { ... function code ... } Placement best place is in the HEAD element recall skeleton must be before first invocation

Function cont'd Functions need not return a value different from VB If a function does return a value, use return value; Scope variables declared with var inside a function are local different from Java all variables in a method are local

Example slide show with functions

The Date object JavaScript has a built-in object classes for programmers to use The Date object used in the homework relevant for doing time and date computations days until Chirstmas in book contains both date and time

Date object, cont'd Constructor Parameters can be new Date (parameters) Parameters can be a single string new Date ("October 27, 2003"); a list of values new Date (2003, 9, 27); empty = current date and time new Date (); Internal representation count of milliseconds

Components of the date Accessors getYear() returns year getMonth () returns month getDay (), getHours(), getMinutes(), getSeconds() Calculations from millisecond representation

Using lastModified date document.lastModified returns a Date in String form not in JavaScript date form Must use the Date constructor new Date (document.lastModified)

Example Greetings How long ago modified?

Homework #6 Case 1 Chapter 8 Zodiac page Meal of the day Use JavaScript prompts to gather date of birth Display zodiac sign

Wednesday Lab 3