1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)

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.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
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
1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content.
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 II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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)
Computers and Scientific Thinking David Reed, Creighton University JavaScript and User Interaction 1.
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.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
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.
Expressions and Data Types Professor Robin Burke.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Scripting
JavaScript.
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
CS105 Introduction to Computer Concepts
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Web Programming– UFCFB Lecture 13
CS105 Introduction to Computer Concepts JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)

2 Outline Intro to JavaScript Intro to JavaScript Fitting JavaScript into HTML Fitting JavaScript into HTML Variables and Operators Variables and Operators Prompting for Input and other Functions Prompting for Input and other Functions Programming Errors Programming Errors Making Decisions with Boolean Logic Making Decisions with Boolean Logic

3 JavaScript JavaScript – Runs within a browser, can manipulate most aspects of HTML JavaScript – Runs within a browser, can manipulate most aspects of HTML webpage.html JavaScript Interpreter Operating System Computer Hardware Browser

4 JavaScript Originally “LiveScape”, developed independent of Java at NetScape by Brendan Eich Originally “LiveScape”, developed independent of Java at NetScape by Brendan Eich A small “scripting” language designed to enhance webpages by manipulating webpage objects A small “scripting” language designed to enhance webpages by manipulating webpage objects Code is embedded in HTML, and called by HTML Code is embedded in HTML, and called by HTML Can manipulated most aspects of a webpage Can manipulated most aspects of a webpage For generation of dynamic content but less complex computation and data manipulation For generation of dynamic content but less complex computation and data manipulation

5 JavaScript Examples: Examples: –Movement of browser windows Movement of browser windowsMovement of browser windows –Validation of entered FORM data Validation of entered FORM dataValidation of entered FORM data –Event handling Event handlingEvent handling

6 Why learn JavaScript? A good first step to learning programming A good first step to learning programming –All fundamental concepts are used –Object-Oriented Programming (OOP) Allows you to quickly build dynamic content for webpages Allows you to quickly build dynamic content for webpages JavaScript code can be saved and reused JavaScript code can be saved and reused Use existing libraries of JavaScript code Use existing libraries of JavaScript code

7 To Eliminate the Annoying Security Message … Open IE Open IE Click Tools, Internet options, Advanced Click Tools, Internet options, Advanced Scroll down to Security Scroll down to Security Enable “Allow active content to run files on My Computer” Enable “Allow active content to run files on My Computer”

8 JavaScript Placement in HTML Can be placed in the BODY or HEAD of an HTML document (most typically in the HEAD.. Why?) Can be placed in the BODY or HEAD of an HTML document (most typically in the HEAD.. Why?)<HTML><HEAD> <!-- document.write(" I'm first, man! "); //--></SCRIPT></HEAD><BODY> OK, buddy. That's fine. OK, buddy. That's fine. <!-- document.write("Last but not least."); //--></SCRIPT></BODY></HTML> Hides JavaScript from browers that cannot interpret it. object - document, method - write() … also know as a function JavaScript statements end with a “;”

9 Variables: Types and Names Variable is a named location in memory Variable is a named location in memory Variables must first be declared: Variables must first be declared: var x; var name,z; Variables can be assigned values: Variables can be assigned values: x = 10; name = “Bubba”; z = x+1; x = x+1; Can be initialized: var x = 10; Can be initialized: var x = 10; Can be of different types of values: Can be of different types of values: –Numeric values such as or 2006 –Character string values as “Please enter ID#” and “2006” Variable names can be composed of: Variable names can be composed of: –letters, numeric digits (except first char), underscore “_” Note the difference

10 Prompting / Storing User Input <HTML><HEAD> <!-- var x; var x; x=prompt("Please enter your name.",""); x=prompt("Please enter your name.",""); document.write(x); document.write(x);//--></script></HEAD> <P></BODY></HTML> Declare variable Assign variable a value, default =“” Use variable in calling of method

11 Math Operators on Variables <HTML><HEAD> <!-- var age; var age; age=prompt("Please enter your age.",""); age=prompt("Please enter your age.",""); var dogage; var dogage; dogage=age*7; dogage=age*7; document.write("You may be "); document.write("You may be "); document.write(age); document.write(age); document.write(" years old but, if you were a dog, you would be "); document.write(" years old but, if you were a dog, you would be "); document.write(dogage); document.write(dogage); document.write(" years old!"); document.write(" years old!");//--></SCRIPT></HEAD><BODY><P></BODY></HTML> Math Operators: + addition - subtraction * multiplication / division

12 Character Operators on Variables Concatenation of strings: Concatenation of strings:<HTML><HEAD> <!-- var dog; var dog; dog="Scooby"+" "+"Doo"; dog="Scooby"+" "+"Doo"; document.write(dog); document.write(dog);//--></SCRIPT></HEAD><BODY><P></BODY></HTML>

13 Beware of Mixing Data Types and Operators <HTML><HEAD> <!-- var age,multiplier,dogyears; var age,multiplier,dogyears; age = "2"; age = "2"; multiplier = "7"; multiplier = "7"; dogyears=age * multiplier; dogyears=age * multiplier; document.write("The dog's age in dog-years is ",dogyears,"."); document.write("The dog's age in dog-years is ",dogyears,".");//--></SCRIPT></HEAD><BODY><P></BODY></HTML> Now try changing the * to a +

14 Converting Strings to Numbers <HTML><HEAD> <!-- var age,multiplier,dogyears; var age,multiplier,dogyears; age = "2"; age = "2"; age = parseFloat(age); age = parseFloat(age); multiplier = "7"; multiplier = "7"; multiplier = parseInt(multiplier); multiplier = parseInt(multiplier); dogyears=age + multiplier; dogyears=age + multiplier; document.write("The dog's age in dog-years is ",dogyears,"."); document.write("The dog's age in dog-years is ",dogyears,".");//--></SCRIPT></HEAD><BODY><P></BODY></HTML> Converts string to floating point number ( ) Converts string to an integer (22, 107) If strings and numbers are mixed then a NaN value can be generated

15 Functions Procedural functions: Procedural functions: –document.write(“hello”); –Returns no value Value functions: Value functions: –parseFloat(“ ”) –Returns a numeric value of –What other value functions have we used?

16 Program Errors Programs almost always have “bugs” Programs almost always have “bugs” “Debugging” programs is a skill “Debugging” programs is a skill Types: Types: –Syntax Errors: document.write(“Hello there”) document.write(“Hello there”) age=prompt(“Enter your age.”); age=prompt(“Enter your age.”); –Logic Errors: Avg=50+100/2; Avg=50+100/2; –User Errors: age=prompt(“Enter your age.”); age=prompt(“Enter your age.”); and user enters “twenty one” and user enters “twenty one”

17 Avoiding Program Errors Design logic of code prior to sitting at the keyboard - draw/write it down in English Design logic of code prior to sitting at the keyboard - draw/write it down in English Have a friend look at the logic Have a friend look at the logic Walk through code pretending to be the computer Walk through code pretending to be the computer Have a friend look at the code Have a friend look at the code Verify input data types.. later Verify input data types.. later

18 Making Decisions – Boolean Variables (Ch.11) Boolean variables or a Boolean expression can have one of two literal values: Boolean variables or a Boolean expression can have one of two literal values: –true or false var x; x = false; x=(a>c); var x; x = false; x=(a>c); True or False ?: True or False ?:x=(1<2);y=(3.14>3.14);z=(3.14>=3.14);X1=((12-3)>((17+3)/2));check=(1==2);fin=(2!=1); != is not equal

19 Making Decisions – Boolean Truth Tables XY&&=AND ||=OR FFFF FTFT TFFT TTTT

20 Making Decisions - Checking Strings <HTML><HEAD> <!-- var num,empty; var num,empty; num=prompt("Please enter your name.",""); num=prompt("Please enter your name.",""); empty=(num==""); empty=(num==""); if (empty) { if (empty) { num=prompt("Please, last chance to enter your name.",""); num=prompt("Please, last chance to enter your name.",""); empty=(num==""); empty=(num==""); } document.write(num,", empty is ",empty); document.write(num,", empty is ",empty);//--></script></HEAD> <P></BODY></HTML>

21 If.. Then.. Else <HTML><HEAD> <!-- var num,empty; var num,empty; num=prompt("Please enter your name.",""); num=prompt("Please enter your name.",""); empty=(num==""); empty=(num==""); if (empty) { if (empty) { num=prompt("Please, last chance to enter your name.",""); num=prompt("Please, last chance to enter your name.",""); empty=(num==""); empty=(num==""); } else { } else { document.write("Great.. you got it the first time!! "); document.write("Great.. you got it the first time!! "); } document.write(num,", empty is ",empty); document.write(num,", empty is ",empty);//--></script></HEAD> <P></BODY></HTML>

22 Further Examples: An Interactive Pizza Order Program An Interactive Pizza Order Program Basic Input Verification Basic Input Verification –Procedure function alert(x) Displays a small window with message X Displays a small window with message X –Value function isNan(x) Returns “TRUE” if x is not a number (NaN) Returns “TRUE” if x is not a number (NaN)

23 Resources

24 THE END