JavaScript Programming Basics Chapter 1. 1.1 What is Programming?

Slides:



Advertisements
Similar presentations
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Advertisements

Lesson 12- Unit L Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Computer Science 103 Chapter 4 Advanced JavaScript.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Introduction to scripting
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
JavaScript Part 1.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
Objective Static vs. Dynamic Web pages. Variables. Assignments. JavaScript Hierarchy of Objects JavaScript Functions (prompt(“”,””) Document.write(“”)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
Tutorial 8 Programming with ActionScript 3.0. XP Objectives Review the basics of ActionScript programming Compare ActionScript 2.0 and ActionScript 3.0.
Using Client-Side Scripts to Enhance Web Applications 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
JavaScript - A Web Script Language Fred Durao
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
XP Tutorial 8 Adding Interactivity with ActionScript.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
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 Challenges Answers for challenges
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Understanding JavaScript and Coding Essentials Lesson 8.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
XP Tutorial 8 Adding Interactivity with ActionScript.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
JavaScript 101 Lesson 6: Introduction to Functions.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
Tarik Booker CS 120 California State University, Los Angeles.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
Training & Development
Tutorial 10: Programming with javascript
Web Programming and Design
Web Programming and Design
Presentation transcript:

JavaScript Programming Basics Chapter 1

1.1 What is Programming?

The Program Development Cycle Analyze the problem – What information are you given? – What information is needed to get desired results? Design a program to solve the problem – Include step-by-step instructions – Can use flowcharts or pseudocode Code the program Test the program At any time you find a flaw, revise!

1.2 The Structure of a Program

Input-Processing-Output Input: prompts previous values other files Processing: what the program does with input and other information Output: display on screen information sent to other places

Prompts Prompt: var name = prompt("Please enter your name"," "); User sees: (Internet Explorer)(Firefox) After entering “Fiona”, the variable name holds the value “Fiona”

Processing the Input Example: var name = prompt("Please enter your name"," "); var greeting = "Hello there, " + name + "!"; If the user enters “Fiona”, the variable name = “Fiona” and the variable greeting = “Hello there, Fiona!”

Output Example: var name = prompt("Please enter your name"," "); var greeting = "Hello there, " + name + "!"; document.write(greeting);

The Control Structures The sequential (or sequence) structure statements execute in sequence The decision (or selection) structure statements execute if a condition is true if not, either nothing happens or other statements execute The loop (or repetition) structure statements execute until a condition is no longer true

The Control Structures The decision (or selection) structure The loop (or repetition) structure

1.3 Data Types and Operations on Data

Numerical Data Numbers are values that can be processed and calculated. Many languages make a distinction between integers and floating point numbers. JavaScript: when a number is stored in a variable, it is initially treated as a floating point number. All numbers in JavaScript are initially stored as the numerical data type. When a number is entered into a prompt box, it is initially stored as a text value. – It cannot be used in a calculation. – It must be turned into a numeric value to use in a calculation.

String Data Strings are a series of keyboard characters enclosed in quotation marks. Strings can consist of words, phrases, sentences, and even whole paragraphs. A string can also be a single character such as a letter or a punctuation character. When a number is stored as a string, it cannot be used in a numerical calculation or process.

Variables and Named Constants A variable is called a variable because it can vary. A quantity that can change value during the execution of a program. Any time we need to refer to that data, we refer to its variable name. A named constant is a value that is used often in a program but will not change value throughout the program, such as the number in a dozen or the tax rate charged on purchases.

Assignment Statements Declaring variables: Use the var keyword var age; creates a variable named age var age = 23; creates a variable named age which is assigned an initial value of 23

Operations on Data Arithmetic Operators: OperatorDescriptionExampleResult, if y = 3 + Additionx = y + 2x = 5 - Subtractionx = y - 2x = 1 * Multiplicationx = y * 2x = 6 / Divisionx = y / 2x = 1.5 % Modulusx = y % 2x = 1

The Concatenation Operator Concatenation Operator: joins two strings together The symbol is + but, by the context, the computer knows that it is not used to add values. Example: greeting = “Good morning" name = “Robbie" The following statement concatenates the variables and other text and stores it as one string in a third variable named welcome : welcome = greeting + “, “ + name; After the execution of this statement, the variable welcome contains : “Good morning, Robbie”

1.4 Problem Solving: The Importance of Logical Thinking

Pseudocode and Flowcharts Pseudocode is a way to think through and design a program before writing the actual code. Pseudocode uses short, English-like phrases to describe the outline of a program. Flowcharts are diagrams that use special symbols to display pictorially the flow of execution within a program or program module. Often programmers use both pseudocode and flowcharts at various stages of a program’s development.

1.5 JavaScript in the Web Page

The Tag Pair : Used to define a client-side script like JavaScript : Used to provide alternate content for users who have disabled scripts in their browsers Used to provide alternate content for browsers that don’t support client-side scripting – rare today Sorry, your browser doesn't support JavaScript.

JavaScript in a Web Page Using inline JavaScript with a button: Code to add a button to a web page: Creates a button that looks like this: When clicked, you get an alert that says: Well, hello, my friend.

JavaScript in the Document Section Most JavaScript we will write will be in section JavaScript executes when user does something in web page Example function welcome() { alert("Hi there, friend!"); } > A New Web Page Click the button! <input type="button“ id="myButton" value="Hi there!" onclick = "welcome();" />

The onload Event Loads JavaScript before user views the page, as it is loading Example function welcome() { alert("Hi there, friend!"); } > A New Web Page This site is nothing but fun!

1.6 Introduction to Objects

What is an Object? Properties and Methods Anything that has properties and a function (or functions) is an object. Properties are qualities, traits, or attributes common to a specific thing—or object. – Properties (also called attributes) describe the object A function, in this context, is a process or operation executed by or to the object. – Methods (also called functions) are the things the object can do or have done to it.

The Document Object An HTML document is an object. It uses the Document Object Model (DOM)

Dot Notation You instruct the browser where to place content by using dot notation. The object is accessed, then a dot, and then any further instructions (methods or attributes) are appended. document.write("Welcome to my first JavaScript page!"); The document object is accessed. The dot says to use the write() method on the document object.

The getElementById() Method Each part of a web page is called an element To access an element use the getElementById() method Allows access a particular container within a document Each container must be marked with an identifier Add an id attribute to the HTML tag

The innerHTML Property The innerHTML property sets or returns the inner HTML of an element Example function getValue() 6. { 7.var dog = document.getElementById("puppy"); 8.document.write("Your dog is not a terrier "); 9.document.write("It is a "); 10.document.write(dog.innerHTML); 11.} Poodle The id of the tag is “puppy” (line 15). Line 6 gets the value of the contents of the element with the id “puppy”. Line 10 uses that value in the output

The open() and close() Methods These methods open or close a new window The innerHTML property sets or returns the inner HTML of an element Using the open() and close() Methods function openWin() 6. { 7. smallWindow = window.open("","", "width=300, height=200"); 8. smallWindow.document.write(" Hi again, old friend! Glad to see you today "); 9. } 10. function closeWin() 11. { 12. smallWindow.close(); 13. } <input type = "button" value = "Close the small window“ onclick = "closeWin()" />

1.7 Introduction to JavaScript Functions and Events

JavaScript Functions A function is used to isolate a group of instructional statements so that other parts of the program can use that code. Functions and methods can normally be used interchangeably. Two main categories of functions: user-created and built-in Syntax to create your own function: – type the function keyword – Follow with the function’s name – Put all statements within opening and closing curly brackets ({ }). function name() { JavaScript statements...; }

Built-in Functions Some built-in JavaScript functions that we have used so far: – alert() – write() – open() – close() – getElementById()

Parameters In general, parameters are values that are passed into a function. Using parameters function calculateTotal(purchaseAmt, taxRate) { tax = purchaseAmt * taxRate; total = purchaseAmt + tax; document.write("Your total is $ "+total); } Amount purchased is $100.00, Tax rate is Click Button 1 to calculate total, passing in , Click Button 2 to calculate the total, passing in 0.065,

The prompt() Function Allows us to prompt the user to input values which can then be used in any way The prompt() Function function showPrompt() { var food = prompt("What's your favorite food?", "carrots and celery"); document.write("It's your lucky day! " + food +" is on today's lunch menu!"); } You first see: After pushing the button: If user enters pizza: If user enters cake:

Introduction to JavaScript Events An event is an action that can be detected by JavaScript Usually events are used in combination with functions When an event occurs, the function is executed Called event-driven programming Events: – a mouse click – a web page or image loading – rolling a mouse over a link, an image, or another hot spot on a web page – selecting an element or a field on a form

Using a Prompt and an Event JavaScript Events function greet() 6. { 7.var name = prompt("Please enter your name"," "); 8.document.write(" Hello " + name + "! How are you today? "); 9. } Who are you? 14. Enter your name

Example Output Initially, this page has a single line and a button and looks like this: If the user presses the button, types in Helmut Lindstrom at the prompt, and presses OK, the page will now look like this: