CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.

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 and the DOM Les Carr COMP3001 Les Carr COMP3001.
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
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript, Third Edition
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.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
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.
Week 9 PHP Cookies and Session Introduction to 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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
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:
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
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.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript, Fourth Edition
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
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.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
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:
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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
“Under the hood”: Angry Birds Maze
JavaScript is a programming language designed for Web pages.
Scope, Objects, Strings, Numbers
Your 1st Programming Assignment
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
Training & Development
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript Basics What is JavaScript?
JavaScript Intro.
Web Programming and Design
Web Programming and Design
CGS 3066: Web Programming and Design Spring 2016
Lecture 9: JavaScript Syntax
JavaScript CS 4640 Programming Languages for Web Applications
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

CGS 3066: Web Programming and Design Spring 2016 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 OperatorMeaningExpression valueExample <Value less thantrue 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 thantrue 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 totrue 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 totrue 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 totrue if both sides have same value and same data type. false otherwise1===2 is false 1===1 is true 1===“1” is false !==Value or type not equal totrue if both sides have mismatched value or data type or both. false otherwise1!==2 is true 1!==1 is false 1!==“1” is true

JavaScript Logical Operators OperatorUsageExpression valueExample &&operand1 && operand2true 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 || operand2true 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 !!operandtrue 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{ alert(counter); 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

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];

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 e.g: //the number of elements in an array named countries. alert(countries.length); //find the index of a particular element in an array. alert(countries.indexOf(“China”); //add a new item at the end of the array countries.push(“South Sudan”); //copy and remove last item from the array var lastitem = countries.pop(); //x is “South Sudan”

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

Objects ● An object is a software representation of a real world entity. ● Objects have properties (whose values identify an object) and methods that act on those properties. ● The values are written as name:value pairs (name and value separated by a colon). var car = {make:"Hynudai", model:”Sonata”, color:"Silver", year:2007};

Object Methods JavaScript methods are the actions that can be performed on objects. A JavaScript method is a property containing a function definition. Methods can be built-in (already available in JavaScript) or user defined. You create an object method with methodName : function() { code lines } You access an object method with objectName.methodName()

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 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: Event attribute JavaScript function

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