CIS 136 Building Mobile Apps

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introducing JavaScript
The Web Warrior Guide to Web Design Technologies
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript
JavaScript, Third Edition
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Programming with JavaScript (Chapter 10). XP Various things Midterm grades: Friday Winter Career Fair – Thursday, April 28, 2011 (11 am to 3 pm). – MAC.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chapter 3: Data Types and Operators JavaScript - Introductory.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
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
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
 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
CIS 133 Mashup Javascript, jQuery and XML Chapter 3 Building Arrays and Controlling Flow.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Server versus Client-Side Programming Server-SideClient-Side.
Introduction into JavaScript Java 1 JavaScript JavaScript programs run from within an HTML document The statements that make up a program in an HTML.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
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.
JavaScript, Sixth Edition
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
>> Introduction to JavaScript
Programming Web Pages with JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Tutorial 10 Programming with JavaScript
JavaScript is a programming language designed for Web pages.
Introduction to Scripting
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Phonegap Bridge Page Events, Physical Events, Device Events
WEB PROGRAMMING JavaScript.
Control Structures Functions Decision making Loops
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
Objectives In this chapter, you will:
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?
An Introduction to JavaScript
Presentation transcript:

CIS 136 Building Mobile Apps More Javascript Code to respond to Page Events, Physical Events, Device Events CIS 136 Building Mobile Apps

A review of Javascript

Using the <script> Element script statements; </script> Each statement inside the script tags is a single line that indicates an action for the browser to take The semicolon notifies the browser that it has reached the end of the statement

Declaring a variable A variable is a value stored in device memory 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. Use reserved keyword var to create variables 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 Assignment operator: equal sign (=) Assigns value on the right side of the expression to the variable on the left side of the expression

Case Sensitivity in JavaScript JavaScript is case sensitive Ignores most occurrences of extra white space Do not break a statement into several lines

Making Decisions Decision making Process of determining the order in which statements execute in a program Decision-making statements, decision-making structures, or conditional statements Used when you need to adopt one path out of the given two paths Conditional statements allow your program to make correct decisions and perform right actions Runs a command or command block only when certain circumstances are met JavaScript supports 2 kinds of conditional statements which are used to perform different actions based on different conditions if .. else switch

if/else Statements Executes one action if the condition is true And a different action if the condition is false Syntax for an if . . . else statement if (expression) { statements } else {

New stuff in Javascript

Adding Comments to a JavaScript Program Nonprinting lines placed in code containing various types of remarks Will be ignored by the browser Line comment Hides a single line of code Add two slashes // before the comment text Block comments Hide multiple lines of code Add /* before the first character included in the block and */ after the last character in the block

Concatenation Combining two pieces of data using the + sign JavaScript is a weakly typed language The + symbol can be used with either numeric values or text strings When both sides of the + sign contain numbers, addition will be performed var total = 5 + 4; var emLink = "cadler" + "@" + "mpl.gov";

switch Statements Controls program flow by executing a specific set of statements based on an expression (one value) until a match is found Examines the expression and then evaluates a series of possible matching values The switch statement is given an expression to evaluate The case statements contain the possible values and the code to execute for each possible value The interpreter checks each case against the value of the expression until a match is found If nothing matches, a default condition will be used. Break statement stops the matching Break statement

Switch switch (americanCity) { case "Boston": alert "Massachusetts"; break; case "Chicago": alert "Illinois"; case "Los Angeles": alert "California"; default: alert "United States"; }

Arrays where one variable holds many values, like a list

Arrays An Array is a Javascript Object It contains one or more items, called elements Each element can hold any type of data Numbers, strings, objects An array can hold many values under a single name

Creating Arrays An array can be created & populated several ways Declaration var cellphones = new Array(); String object’s split method: var cellphones=dataString.split(',');

Declaring and Initializing Arrays (cont’d.) Element Each piece of data contained in an array Index Element’s numeric position within the array Array element numbering Starts with index number of zero (0) To access data in an array, refer to the arrayname followed by its index cellphones[0];

Strings

Manipulating Strings String var txt = “special text"; Text contained within double or single quotation marks To create strings var txt = “special text"; String Class is used to manipulate text parse (extract) text strings in scripts Format text Find text patterns Replace text Chop up text

Split method split()- Splits a string into an array of substrings var str = "How are you doing today?"; var res = str.split(" "); Results in an array namede res Res[0] holds the value How Res[1] holds the value are

Controlling when code executes Functions Controlling when code executes

Functions A JavaScript function is a block of code designed to perform a particular task – an action of some sort A JavaScript function is executed when "something" invokes it (calls it). Functions must be contained within a <script> element Function statements do the actual work and are contained within curly braces

Defining Functions Syntax function nameOfFunction(parameters) { statements; } Parameter Variable used within a function Placed within parentheses following a function name Multiple parameters allowed function calc_square_root(number1, number2) { var sr1 = number1 * number1

Lets Practice Define a function named “displayMyBank” Inside the function, use a an alert statement, to write out the name of your bank The function does not need parameters Syntax function averageNumbers(a, b, c) { var sum_of_numbers = a + b + c; var result = sum_of_numbers / 3; }

Calling Functions Functions can be written anywhere in the web page, but are usually within the document head, or an external .js file Calls to the function are usually within the body section and consist of the function name followed by parentheses function printStudentNames() { navigator.notification.alert(“<p>” + Joe + “</p>”); navigator.notification.alert(“<p>” + Sam + “</p>”); navigator.notification.alert(“<p>” + Bob + “</p>”); } </script> </head> <body> <script> printStudentNames(); </script>

Calling Functions using parameters If a function needs additional information, providing it is called Passing arguments The function must be designed to receive that data, and that data is called a parameter Arguments vs. Parameters – refer to the same memory location, therefore the same data function averageNumbers(a, b, c) { var sum_of_numbers = a + b + c; var result = sum_of_numbers / 3; } averageNumbers(1,2,3);