CGS 3066: Web Programming and Design Fall 2019

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

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
The Web Warrior Guide to Web Design Technologies
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Computer Science 103 Chapter 4 Advanced JavaScript.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
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.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
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.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Using Client-Side Scripts to Enhance Web Applications 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Intro to JavaScript Scripting language –ECMAScript compliant –Client side vs. server side Syntactic rules –White space –Statement end: ; optional –Comments:
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript, Fourth Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript in Context. Server-Side Programming.
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.
JavaScript Functions A function is a block of code that can be reused. It is similar to a method in Java. It consists of a function name, an argument.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Tarik Booker CS 120 California State University, Los Angeles.
CGS 3066: Web Programming and Design Spring 2017
CGS 3066: Web Programming and Design Spring 2017
>> Introduction to JavaScript
JavaScript and HTML Simple Event Handling 11-May-18.
Programming Web Pages with JavaScript
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Chapter 4 Client-Side Programming: the JavaScript Language
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
JavaScript is a programming language designed for Web pages.
SEEM4570 Tutorial 05: JavaScript as OOP
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
JavaScript and HTML Simple Event Handling 19-Sep-18.
Objectives Insert a script element Write JavaScript comments
Your 1st Programming Assignment
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
CS5220 Advanced Topics in Web Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript Basics What is JavaScript?
JavaScript Intro.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
CIS 136 Building Mobile Apps
CS3220 Web and Internet Programming JavaScript Basics
Web Programming and Design
Web Programming and Design
CGS 3066: Web Programming and Design Spring 2016
JavaScript and HTML Simple Event Handling 26-Aug-19.
Lecture 9: JavaScript Syntax
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

CGS 3066: Web Programming and Design Fall 2019 Programming in JavaScript

Expressions  An expression is any piece of code that resolves to a value(numeric/string/Boolean etc.) We can combine literal values, variables and operators to create complex expressions 5,”ABC”,true, x, 1+7, 3+x, 2*x+3, x>5, x=4 are all expressions (given that x is a variable) Expression that uses assignment(e.g. =,+= etc.) operator, resolves to the value that is finally assigned to left side variable Expression that compares two value/expressions using a relational operator, resolves to either true or false Expression that applies logical operator(&&, ||, !) over values/other expressions, resolves to either true or false

Expressions(contd.) var x,y; x=10 //expression value is 10 y=7 //expression value is 7 x+4 //expression value is 14, x is still 10 x+=y//equivalent to x=x+y; expression value is 17, x is 17 y=x-2 //expression value is 15, y is 15 Create your own expression and test its value using document.writeln() function

JavaScript Relational Operators Meaning Expression value Example < Value less than true if left-side operand has a value smaller than that of the right-hand operand. false otherwise 5<10 is true 10<9 is false 10<10 is false > Value greater than true if left-side operand has a value greater than that of the right-hand operand. false otherwise 5>10 is false 10>9 is true 10>10 is false <= Value less than or equal to true if left-side operand has a value smaller than or equal to that of the right-hand operand. false otherwise 5<=10 is true 10<=9 is false 10<=10 is true >= Value greater than or equal to true if left-side operand has a value greater than or equal to that of the right-hand operand. false otherwise 5>=10 is false 10>=9 is true 10>=10 is true == Value equal to true if left-side operand has the same value as the right-hand operand. false otherwise 1==2 is false 1==1 is true 1==“1” is true != Value not equal to true if left-side operand has a different value than that of the right-hand operand. false otherwise 1!=2 is true 1!=1 is false 1!=“1” is false === Value and type equal to true if both sides have same value and same data type. false otherwise 1===2 is false 1===1 is true 1===“1” is false !== Value or type not equal to true if both sides have mismatched value or data type or both. false otherwise 1!==2 is true 1!==1 is false 1!==“1” is true

JavaScript Logical Operators Usage Expression value Example && operand1 && operand2 true if both operands are,or can be converted to true. false otherwise true && true is true true && false is false false && true is false false && false is false || operand1 || operand2 true if either one of the two operands is true,or can be converted to true. false if both operands are false true || true is true true || false is true false || true is true false || false is false ! !operand true if left-side expression has a value smaller than or equal to that of the right-hand expression. false otherwise !(true) is false !(false) is true

JavaScript falsy values The following data values are considered ‘falsy’, equivalend to Boolean false 0 (numeric) false (Boolean) “” or ‘’ (empty string of characters) null undefined NaN (Numeric values indicating Not a Number)

Entering special characters in document.writeln() \’ - prints single quote \” - prints single quote \\ - prints backslash \n - new line \t – tab \r - carriage return \b - backspace

Conditional Statement Allows decision-making based on logically described criteria You choose to run one piece of code depending on some logical condition. Such piece of code and the condition that triggers it, is described in a conditional statement If statement: if some condition is true, run a block of code; otherwise, run another block of code.

Conditional Statement If statement: if ( 10 > 5 ){ //run the code here } If … else… statement: if( 10 > 5 ){ //run the code here } else{ //run a different piece of code here}

Loop Statement Loop is a way of repeating the same block of code over and over. While loop: repeats a block of code while a condition is true. var counter = 1; while(counter < 10){ alert(counter); counter++; //counter = counter + 1; } Do-while loop: specify looping condition after the statement block Executes at least once do{ counter++;} while(counter < 10);

For Statement For loop: it combines three semicolon-separated pieces information between the parentheses: initialization, condition and a final expression. for(var counter = 1; counter < 10; counter++) { alert(counter); }

Array An array is used to store multiple values in a single variable. An array is just one variable that contains a list of values. e.g., var numbers = new Array(); numbers[0] = 12; numbers[1] = 15; numbers[2] = 45; numbers[3] = 22; index value 12 15 1 2 45 3 22

Create and Access An Array var trees = new Array(“maple”, “ashley”, “oak”); var countries = [“America”, “Spain”, “China”]; Access an array: you refer to an element in an array using the index. var myCountry = countries[0]; Methods: concat, join, pop, push, reverse, shift, slice, sort, splice, toString, unshift

Array Methods and Properties An array has predefined properties and methods Use dot(.) operator to access property/method of a given array Methods require appropriate input parameter in parantheses var countries = [“USA”, “Spain”, “China”]; countries.length; //3 countries.indexOf(“China”); //2 countries.push(“Japan”); // USA, Spain, China, Japan var lastitem = countries.pop(); //countries = USA, Spain, China; //lastitem = Japan countries.unshift(“Korea”); // Korea, USA, Spain, China countries.shift(); // USA, Spain, China countries.sort(); // China, Spain, USA

JavaScript Functions A function is a block of code that can be reused. It consists of a function name, a parameter list, and code that is executed when the function is called. When needed, we can call a javascript function by it’s name and provided a list of arguments(to be mapped to parameters) Functions may also be called automatically on web events(e.g. clicking a button) After the code inside the function are executed, execution of code resumes after the calling context. Function may return values back to the calling context

Function Example //declaration of function RectangleArea. function RectangleArea (height,width){ return height*width; }//end of function declaration //this is a function call var area = RectangleArea(100,40); alert(area); //code execution resumes here after function call Function name Parameter list Function body Argument list

JavaScript Objects Three types of objects: Native objects: defined by JavaScript. String, Number, Array, Image, Date, Math, etc. Host objects : supplied and always available to JavaScript by the browser environment. window, document, forms, etc. User-defined objects : defined by the author/programmer

Working with Objects 1. Using the Object literal syntax: var car = { make : "Toyota", model : "Sienna", year : "2015", displayInfo : function(){ console.log(`Sam drives a ${car.make} ${car.model} ${car.year}`); } car.displayInfo(); var car1 = { console.log(`Sam drives a ${this.make} ${this.model} ${this.year}`); car1.displayInfo();

Working with Objects 2. Object Constructor: var car = new Object(); car.make = "Toyota"; car.model = "Sienna"; car.year = 2015; car.displayInfo = function(){ console.log(`Sam drives a ${car.make} ${car.model} ${car.year}`); } car.displayInfo();

Working with Objects 3. Constructor function: function Car(make, model, year) { this.make = make; this.model = model; this.year = year; this.displayInfo = function(){ console.log(` Sam drives a ${this.make} ${this.model} ${this.year}`); } var car = new Car("Honda", "Pilot", 2018); car.displayInfo(); var car2 = new Car("Hyundai", "Sonata", 2017); car2.displayInfo();

Working with Objects 4. Prototypes: Every JS object has a default prototype object property. Another object or method can be attached to this prototype. var car = { make : "Toyota", model : "Sienna", year : "2015", displayInfo : function(){ console.log(`Sam drives a ${this.make} ${this.model} ${this.year}`); } }; var mycar = Object.create(car, displayInfo); mycar.displayInfo();

Accessing Objects 1. Dot notation: obj.property car.make car.model 2. Bracket notation: obj[“property”] car[“make”] car[“year”]

DOM Document Object Model (DOM) is a programming interface to access and manipulate the HTML document. Describes the HTML document as an object, consisting of properties and methods DOM structure is maintained by the browser, accessible from Javascript through the object name document HTML elements also viewed as objects, contained inside(properties of) document From Javasvcript, the DOM

Example DOM methods Finding HTML Elements Modifying HTML Elements document.getElementById(“ID_VALUE”) document.getElementsByName(“NAME_VALUE”) document.getElementsByClassName(“CLASSNAME”) document.querySelector(“CSS-SELECTOR”) Modifying HTML Elements document.write(“text”) document.getElementById(“sectionOne”).innerHTML = “” document.getSelector(“.paraOne”).innerHTML = “”

Changing properties of an element You can set the attribute of a given element dynamically by using the property of the object. objectname.attributename = value; For example: var element = window.document.getElementById(“img1”); var photoName = “fsu.JPG"; element.src = photoName;

SetAttribute() of a object You can also set the attribute of a given element by using the method setAttribute(attributeName, Value) SetAttribute is a method of an element type object Same example: var element = window.document.getElementById(“img1”); var photoName = “fsu.JPG"; element.setAttribute("src", photoName);

Javascript Event Handling Browser-based JavaScript programs are event-driven. This means that a function is called in response to various user actions. An event in a browser is an occurrence of potential interest. The mouse moving over an element A mouse button being clicked A key being pressed

Intrinsic Event Attribute An intrinsic event attribute is used to call script functions when a given event associated with the element containing the attribute occurs. for example: <button type="button" onclick="ChangeParagraph('para2')"> JavaScript function Event attribute

Common Intrinsic Event Attributes onload – the body of the document has been fully read and parsed onclick – a mouse button has been clicked and released over the element onchange – An HTML element has been changed onmousedown – the mouse has been clicked over the element onmouseup – the mouse has been release over the element onmouseover – the mouse has just moved over the element onkeypress – this element has the focus and a key has been pressed & released onkeydown – this element has the focus and a key has been pressed onkeyup – this element has the focus and a key has released