CITS1231 Web Technologies. JavaScript

Similar documents
What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

Working with JavaScript

<form>. input elements. </form>

CGS 3066: Spring 2015 JavaScript Reference

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

JavaScript Introduction

CHAPTER 6 JAVASCRIPT PART 1

Introduction to JavaScript

CISC 1600 Lecture 2.4 Introduction to JavaScript

CSC Web Programming. Introduction to JavaScript

JavaScript: Introduction, Types

Syllabus - July to Sept

Programming language components

Web Programming/Scripting: JavaScript

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

The first sample. What is JavaScript?

Session 6. JavaScript Part 1. Reading

Session 16. JavaScript Part 1. Reading

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

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

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

Princess Nourah bint Abdulrahman University. Computer Sciences Department

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

JavaScript by Vetri. Creating a Programmable Web Page

JavaScript s role on the Web

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

Chapter 2 Working with Data Types and Operators

JavaScript CS 4640 Programming Languages for Web Applications

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

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

Tutorial 10: Programming with JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

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

JavaScript Introduction

JavaScript I Language Basics

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

COMP519 Practical 5 JavaScript (1)

introjs.notebook March 02, 2014

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

Client vs Server Scripting

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

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

JavaScript: The Basics

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Introduction to Python

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

CSCE 120: Learning To Code

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

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

Introduction to Programming Using Java (98-388)

5. JavaScript Basics

Exercise 1: Basic HTML and JavaScript

Client-Side Web Technologies. JavaScript Part I

Chapter 3 Data Types and Variables

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

ITEC136 - Lab 2 Population

(Refer Slide Time: 01:40)

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

By the end of this section of the practical, the students should be able to:

A Balanced Introduction to Computer Science, 3/E

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

JAVASCRIPT MOCK TEST JAVASCRIPT MOCK TEST I

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Coding in JavaScript functions

CS312: Programming Languages. Lecture 21: JavaScript

PHP. Interactive Web Systems

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

Outcomes Week 2 Overview Describe the inputs, activities, and outputs of each step in the software development life cycle. Describe arithmetic, relati

CS313D: ADVANCED PROGRAMMING LANGUAGE

Web Programming HTML CSS JavaScript Step by step Exercises Hans-Petter Halvorsen

Chapter 17. Fundamental Concepts Expressed in JavaScript

Q1. What is JavaScript?

a Why JavaScript? jonkv interactivity on the web CGI JavaScript Java Applets Netscape LiveScript JavaScript 1: Example

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

COMS 469: Interactive Media II

Chapter 2: Using Data

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

I. Variables and Data Type week 3

Programming with Java

JavaScript: Introductionto Scripting

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

SEEM4570 System Design and Implementation Lecture 03 JavaScript

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2. 2 Working with JavaScript

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

CS1 Lecture 3 Jan. 22, 2018

JavaScript Introduction

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

Shwetank Kumar Gupta

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Variables and Typing

Transcription:

CITS1231 Web Technologies JavaScript

Contents Introduction to JavaScript Variables Operators Conditional Statements Program Loops Popup Boxes Functions 2

User Interaction User interaction requires web page content to change. Web server needs to send new web pages. Delays in communicating with server (slow interaction). Server load goes up (cost and delay). User experience goes down. Cost of servers can go up. Recall fast client side image maps versus slow server side maps. But this interaction is just hyperlinks. How can we have richer user interaction that is fast? 3

Introduction to JavaScript Designed to add interactivity to HTML page Client side processing means fast response Can generate new HTML code on the fly without server interaction Most popular scripting language on the internet Scripting language is a lightweight programming language Usually embedded directly into HTML pages Works in all major browsers No license required 4

Java and JavaScript Are Java and JavaScript the same? NO! They are two completely different languages! Java is much more complex (as per C and C++). JavaScript s official name is ECMAScript Invented by Brendan Eich at Netscape (with Navigator 2.0) developed and maintained by the ECMA organization international ISO (ISO/IEC 16262) standard (1998) 5

What Can JavaScript Do? Gives HTML designers a programming tool with a very simple syntax! Dynamically generate text and put into an HTML page. React to events (egwhen page completes loading, user clicks on HTML element). Read and change the content of an HTML element. Validate form data before submission to server. Detect the visitor's browser (and load another page specificallydesigned for that browser). Store and retrieve information on the visitor's computer (cookie). 6

Example: Hello World <html> <body> <script type="text/javascript"> document.writeln("<h1>hello CITS1231 Students!</h1>"); </script> </body> </html> 7

JavaScript Element General form: <script type="text/javascript"> JavaScript Commands </script> JavaScript is Case Sensitive (unlike HTML) Watch your capitalization closely when you write JavaScript statements. 8

JavaScript Comments Use comments to explain your JavaScript. Single line comments start with //. Multi line comments start with /*and end with */. <script type="text/javascript"> // // this script is used to illustrate the use of comments // //document.writeln( <p>this paragraph has been commented out"); document.writeln("<p>normal paragraph</p>"); /* document.writeln("<p>this paragraph commented out using multi-line</p>"); document.writeln("<p>another paragraph commented out using multi-line</p>"); */ document.writeln("<p>another paragraph</p>"); // comments at end of line </script> You can use comments at end of line. 9

Browsers Without JavaScript Some simple browsers don t support JavaScript. They will just display the JavaScript as page content. Use HTML comments to prevent this: <script type="text/javascript"> <!-- document.writeln("<h1>hello CITS1231 Students!</h1>"); //--> </script> The // is a JavaScript comment symbol -prevents JavaScript from executing -->. More later. 10

JavaScript Variables: Revision High School Algebra Revision: x=1, y=6, z=2*x+y A letter (like x) can be used to hold a value (like 1). These letters are called variables. Variables can be used to hold values (x=1) or expressions (z=2*x+y). In the above example, the value of variable z is 8. 11

JavaScript Variables As per algebra, JavaScript variables can hold values or expressions. Can have short names (egx), or descriptive names (egincome). Names are case sensitive (z and Z are different variables). Names must begin with a letter or the underscore character. Use the var statement to create/declare a variable: var x; // not initialized; varz = 100; var_myincome = 1000; var_yourincome = _myincome + z; As per algebra, you can do arithmetic with Java variables. _yourincome evaluates to 1100. 12

Identifiers Identifiers are the names of your variables, functions and objects. Try to make them meaningful, e.g. name rather than x21 Function Identifiers must be unique within a page. They must start with a letter or underscore and can contain letters and the digits 0-9 Valididentifiers: Fred29 _foo AbCd Invalid identifiers: hello! 97abc They cannot include spaces, or be a JavaScript keyword. 13

Variables A variable is an identifiable or named element (often referred to as an identifier). It is used to store and retrieve data. radius = 2.54; ThisMonth = May ; Once the variable is assigned a value, referring to the name is equivalent to referring to its current value. To use a variable it must be declared (created). This is done inone of 3 ways 14 varidentifier; eg varradius; var identifier = value; eg var day = Friday ; identifier = value; eg c_to_i = 2.54; = sign is used for assigning a value to a variable. Don t confuse with == which is used for comparison in JavaScript (see later).

JavaScript Variables The power of programming comes from being able to store, reuse and manipulate values in variables JavaScript variables should be declared varvariable; Then they can be assigned, eg var username= mark ; And processed document.writeln(username + @ + deptaddr); Variables can store strings, numbers, booleans, or null. 15

An Example 16 <tr> <td>dr Fred Lecturer</td> <td>cs1.15</td> <td> <script type="text/javascript"> var username="fred"; var deptaddr="csse.uwa.edu.au"; var addr=username+"@"+deptaddr; document.writeln("<a href='mailto:"+addr+"'>"); document.writeln(addr); document.writeln("</a>"); </script> </td> </tr>

Variables and Literals There are two different ways to use values in your scripts. Variables hold data that can be changed at any time Literals are fixed values that don t change (eg 1, blah ). Values have an associated type. Some important JavaScript types are: integers (27) floating point numbers (42.03948208) strings ( hello ) booleans(true or FALSE) JavaScript is not strong typed, you do not need to declare the variable type. Eg: 17 varx = 27; vary= 42.03948208; varz= hello ; vart=true;

Arithmetic Operators 18

Unary Operators 19

Assignment Operators 20

Conditional Statements Allow you to perform different actions for different decisions. ifstatement used to execute some code if specified conditions true. if...elsestatement execute some code if the condition is true and another code if the condition is false if...else if...elsestatement select one of many blocks of code to execute switch statement select one of many blocks of code to execute 21

if Statement Syntax Example if (condition) { commands } x = 3; // x is assigned value 3 if (x==3) // test if x has value 3 { document.writeln( Yes x equals 3 ); } 22

if else Statement Example x = 4; // x is assigned value 3 if (x==3){ // test if x has value 3 { document.writeln( Yes x equals 3 ); } else // all other cases { document.writeln( No x does not equal 3 ); } 23

if else if else Statement Example x = 4; // x is assigned value 3 if (x==3){ // test if x has value 3 { document.writeln( Yes x equals 3 ); } else if (x==4) // test if x has value 4 { document.writeln( x equals 4 ); } else // all other cases { document.writeln( x does not equal 3 nor 4 ); } 24

switch Statement Syntax switch(condition) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: for all other cases, execute code default block } 25

Example: switch Example switch(x) { case 3: document.writeln( x equals 3 ); break; case 4: document.writeln( x equals 4 ); break; default: document.writeln( x neither equals 3 nor 4 ); } 26

Comparison Operators 27

Logical Operators 28

Program Loops Allow you to execute same block of code repeatedly. forloop: loops through block of code a specific number of times while loop: loops through block of code while a specified condition is valid 29

Working for Loop Syntax: for (initial;test;iterate) { code to be executed } initial is an expression evaluated before the program loop. testis an expression evaluated at the start of each loop. The loop is terminated if test evaluates to false. iterateis an expression that is evaluated at the end of each loop. 30

Counter Values in the For loop 31

Example: for loop <script type="text/javascript"> var i=0; for (i=0;i<=5;i++) { document.writeln("the number is " + i); document.writeln("<br/>"); } </script> 32

Example: while loop <script type="text/javascript"> var i=0; while (i<=5) { document.writeln("the number is " + i); document.writeln("<br/>"); i++; } </script> 33

JavaScript Popup Boxes JavaScript has three kind of popup boxes Alert box Syntax: alert( sometext ); Pops up a box with text. Prompt box Syntax: prompt( question, defaultvalue ); Pops up a box with question and obtains user s answer. The answer will be pre-filled with defaultvalue. Confirm box Syntax: confirm( question ); Pops up a box with question. The user can choose Ok or Cancel. 34

Example: Alert Popup <body> <script type="text/javascript"> alert( This is an example of an alert box ); </script> </body> http://undergraduate.csse.uwa.edu.au/units/cits1231/lectures/lecture11_alert.html 35

Example: Prompt Popup <body> <pre> <script type="text/javascript"> nn=prompt( What is your name?, patrick ); document.writeln( Hello +nn); document.writeln( Hope your day is going well! ); </script> <pre> </body> http://undergraduate.csse.uwa.edu.au/units/cits1231/lectures/lecture11_prompt.html 36

Example: Confirm Popup <body> <pre> <script type="text/javascript"> gender = confirm("press a button"); if (gender==true) { document.writeln("youpressed OK."); } else {document.writeln("youpressed Cancel."); } </script> </pre> </body> http://undergraduate.csse.uwa.edu.au/units/cits1231/lectures/lecture11_confirm.html 37

JavaScript Functions Contains code to be executed by an event or by a call to the function. Browser does not automatically execute code inside function unless the function is explicitly called. Can be called from anywhere within a page. Can be embedded in an external.jsfile. Can be in the <head> or <body>. Better to put in <head> so it is available to whole page. 38

JavaScript Function Syntax: function functionname(var1,var2,...,varn) { code } Example: <head> <script type="text/javascript"> function handleclick() { alert("youclicked the button. Well done!"); } </script> </head> <body> <form> <input type="button" value="click here!" onclick="handleclick()" /> </form> </body> http://undergraduate.csse.uwa.edu.au/units/cits1231/lectures/lecture11_function.html 39

Summary JavaScript can provide fast and rich user interaction. Variables Operators Conditional Statements Program Loops Popup Boxes Functions 40