Working with JavaScript

Similar documents
INFS 2150 Introduction to Web Development and e-commerce Technology. Programming with JavaScript

Objectives. Introduction to JavaScript. Introduction to JavaScript INFS Peter Y. Wu, RMU 1

JavaScript by Vetri. Creating a Programmable Web Page

Chapter 2 Working with Data Types and Operators

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Tutorial 10: Programming with JavaScript

CHAPTER 6 JAVASCRIPT PART 1

COMS 469: Interactive Media II

JavaScript CS 4640 Programming Languages for Web Applications

CSC Web Programming. Introduction to JavaScript

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

JavaScript I Language Basics

CGS 3066: Spring 2015 JavaScript Reference

Full file at

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

CITS1231 Web Technologies. JavaScript Math, String, Array, Number, Debugging

LECTURE 02 INTRODUCTION TO C++

JavaScript CS 4640 Programming Languages for Web Applications

CITS1231 Web Technologies. JavaScript

Lesson 1: Writing Your First JavaScript

JavaScript: The Basics

5/19/2015. Objectives. JavaScript, Sixth Edition. Introduction to the World Wide Web (cont d.) Introduction to the World Wide Web

Outline. Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages

<script type="text/javascript"> script commands </script>

Pace University. Fundamental Concepts of CS121 1

Chapter 17. Fundamental Concepts Expressed in JavaScript

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

INF5750. Introduction to JavaScript and Node.js

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

Technical Questions. Q 1) What are the key features in C programming language?

Visual C# Instructor s Manual Table of Contents

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

JavaScript: Introduction, Types

Full file at C How to Program, 6/e Multiple Choice Test Bank

3 The Building Blocks: Data Types, Literals, and Variables

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

JavaScript: Introduction to Scripting

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN

Full file at

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Arrays Structured data Arrays What is an array?

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

CISC 1600 Lecture 2.4 Introduction to JavaScript

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture Set 2: Starting Java

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

What is PHP? [1] Figure 1 [1]

Lecture Set 2: Starting Java

Computer Programming : C++

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Dynamism and Detection

Chapter 2. Lexical Elements & Operators

JME Language Reference Manual

CSE 142/143 Unofficial Style Guide

EXERCISE: Introduction to client side JavaScript

Introduction to Bioinformatics

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

c122mar413.notebook March 06, 2013

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

Chapter 2 Basic Elements of C++

Decision Making in C

Client-Side Web Technologies. JavaScript Part I

Variables and Typing

JavaScript Basics. The Big Picture

CHAPTER 3 BASIC INSTRUCTION OF C++

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Language Reference Manual

Chapter 2 Author Notes

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Introduction to Java & Fundamental Data Types

CSI33 Data Structures

Session 3: JavaScript - Structured Programming

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

Chapter 2: Using Data

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMS 469: Interactive Media II

Chapter 2: Functions and Control Structures

JavaScript: Introductionto Scripting

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

The C++ Language. Arizona State University 1

XQ: An XML Query Language Language Reference Manual

Chapter 2 Primitive Data Types and Operations. Objectives

Overview of the Ruby Language. By Ron Haley

CSCE 120: Learning To Code

ARG! Language Reference Manual

In addition to the primary macro syntax, the system also supports several special macro types:

CMPT 100 : INTRODUCTION TO

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Transcription:

Working with JavaScript Creating a Programmable Web Page for North Pole Novelties 1

Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page 2

Objectives Working with Variables and Data Working with Dates Working with Expressions and Operators Creating JavaScript Functions Creating the XmasDays Function 3

Objectives Working with Conditional Statements Using Arrays Working with Program Loops Debugging Your JavaScript Programs 4

Introduction to JavaScript Server-side Programs pose problems Client-side Programs were developed to run programs and scripts on the client side of a Web browser 5

The Development of JavaScript JavaScript is a subset of Java Differences between Java and JavaScript: Java is a compiled language JavaScript is an interpreted language 6

Comparing Java and JavaScript 7

The Development of JavaScript Jscript is a version of JavaScript supported by Internet Explorer The European Computer Manufacturers Association (ECMA) develops scripting standards The standard is called ECMAScript but browsers still generally call is JavaScript 8

Inserting JavaScript into a Web Page File Outline the main tasks you want the program to perform first A JavaScript program can either be placed directly in a Web page file or saved in an external text file 9

Inserting JavaScript into a Web Page File Insert a client-side script in a Web page when using the script element Comments are useful for hiding scripts from older browsers Specify alternative content using the nonscript element for browsers that don t support scripts (or have their script support disabled) 10

Writing Output to the Web Page An object-oriented programming language writes the output by manipulating tasks An action you perform on an object is called a method 11

Writing Output to the Web Page To write text to a Web page, use the following JavaScript commands: document.write( text ); or document.writeln( text ) Where text is the content to be written to the page. The doucment.write() and document.writeln() methods are identical, except that the document.writeln() method preserves any line breaks in the text string. 12

Working with Variables and Data A variable is a named item in a program that stores information Variable names have restrictions (see pages HTML 531-532) 13

Working with Variables and Data JavaScript variable types: Numeric variables String variables Boolean variables Null variables You must declare a variable before using it 14

Working with Variables and Data Numeric variable- any number, such as 13, 22.5, etc Can also be expressed in scientific notation String variable- any group of text characters, such as Hello or Happy Holidays! Must be enclosed within either double or single quotations (but not both) Boolean variable- accepts only true and false values Null variable- has no value at all 15

Declaring a JavaScript Variable You can declare variables with any of the following JavaScript commands: var variable; var variable = value; variable = value; Where variable is the name of the variable and value is the initial value of the variable. The first command creates the variable without assigning it a value; the second and third commands both create the variable and assign it a value. 16

Working with Dates Create a date object to store date information Date Methods 17

Working with Expressions and Operators Expressions are JavaScript commands that assign values and variables Operators are elements that perform actions within expressions Arithmetic operators: perform simple mathematical calculations Binary operators: work on two elements in an expression Unary operators: work on only one variable Increment operators: can be used to increase the value of a variable by 1 Assignment operators: used to assign values in expressions 18

Working with Expressions and Operators The Math object is a JavaScript object used for calculations other than simple math 19

Creating JavaScript Functions A function is a series of commands that perform an action or calculates a value A function name identifies a function Parameters are values used by the function 20

Creating JavaScript Functions A group of commands set off by curly braces is called a command block. Command blocks exist for other JavaScript structures in addition to functions. 21

Working with Conditional Statements Conditional statements are commands that run only when specific conditions are met Conditional statements require a Boolean expression you need one of the following operators to create a Boolean expression: Comparison operator Logical operator Conditional operator 22

Using Arrays An array is an ordered collection of values referenced by a single variable name var variable = new Array (size); Where variable is the name of the array variable and size is the number of elements in the array 23

Working with Program Loops A program loop is a set of instructions that is executed repeatedly Use program loops to configure a group of commands to be executed a set number of times The loop uses a counter to track the number of times the command block has been run 24

Working with Program Loops Creating a For loop 25

Working with Program Loops Nesting a For loop 26

Working with Program Loops Creating a While loop 27

Working with Program Loops Nesting a While loop 28

Debugging Your JavaScript Programs Three types of errors: Load-time errors (occurs when the script is loading) Run-time errors (occurs when the being executed) Logical errors (free from syntax and structural mistakes, but result in incorrect results) 29

Common Mistakes You need to debug your program to fix the mistakes Common mistakes include: Misspelling a variable name Mismatched parentheses or braces Mismatched quotes Missing quotes Using ( instead of [ Using = in place of == 30

Debugging Tools and Techniques To avoid making mistakes and quickly located those you do make: Write modular code Use the Microsoft Script Debugger to debug (for use with Internet Explorer) Use the Netscape JavaScript Console to debug (for use with Netscape) 31

Tips for Writing Good JavaScript Code Use good layout to make your code more readable. Indent command blocks to make them easier to read and to set them off from other code Use descriptive variable names to indicate the purpose of your variables Be careful how you use uppercase and lowercase letters in your code, because JavaScript commands and names are casesensitive 32

Tips for Writing Good JavaScript Code Add comments to your code to document the purpose of each script Initialize all variables at the top of your script and insert comments describing the purpose and nature of your variables Create customized functions that can be reused in different scripts. Place your customized functions in external files to make them available to your entire Web site 33