JavaScript MCS/BCS.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
>> Introduction to JavaScript
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Introduction to JavaScript Gordon Tian
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 JavaScript Part 3. Functions Allow the user to decide when a particular script should be run by the browser in stead of running as long as the page.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Understanding JavaScript and Coding Essentials Lesson 8.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
1 JavaScript and Dynamic Web Pages Lecture 7. 2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
REEM ALMOTIRI Information Technology Department Majmaah University.
Servlets What is a Servlet?
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Web Systems & Technologies
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Learning to Program D is for Digital.
A little MORE on JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Intro to JavaScript CS 1150 Spring 2017.
JAVA Script : Functions Ashima Wadhwa
Barb Ericson Georgia Institute of Technology May 2006
Introduction to Web programming
Functions Comp 205 Fall ‘17.
JavaScript: Functions
JavaScript Functions.
My First Web Page document.write("" + Date() + ""); To insert.
JavaScript Syntax and Semantics
Retrieving information from forms
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript: Functions.
DHTML.
Understanding JavaScript and Coding Essentials
Chapter 4 - Visual Basic Schneider
Event Driven Programming & User Defined Functions
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
JavaScript What is JavaScript? What can JavaScript do?
2017, Fall Pusan National University Ki-Joune Li
Javascript.
JavaScript What is JavaScript? What can JavaScript do?
Training & Development
E-commerce Applications Development
Chapter 20: Programming Functions
Java Script Siddharth Srivastava.
JavaScript – Functions
JavaScript: Introduction to Scripting
Web Programming Anselm Spoerri PhD (MIT) Rutgers University
Introduction to Web programming
Retrieving information from forms
Presentation transcript:

JavaScript MCS/BCS

JavaScript Output JavaScript can "display" data in different ways: Writing into the HTML output using document.write(). Writing into an alert box, using window.alert(). Writing into an HTML element, using innerHTML. Writing into the browser console, using console.log().

Using innerHTML To access an HTML element, JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content: <html> <body> <h1>My First Web Page</h1> <p>My First Paragraph</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 + 6; </script> </body> </html>

JavaScript Functions A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). JavaScript Function Syntax A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...) The code to be executed, by the function, is placed inside curly brackets: {}

Cont… function name(parameter1, parameter2, parameter3) {     code to be executed } Function parameters are listed inside the parentheses () in the function definition. Function arguments are the values received by the function when it is invoked. Inside the function, the arguments (the parameters) behave as local variables.

Function Invocation The code inside the function will execute when "something" invokes (calls) the function: When an event occurs (when a user clicks a button) When it is invoked (called) from JavaScript code Automatically (self invoked)

Function Return When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value. The return value is "returned" back to the "caller":

Example: Calculate the product of two numbers, and return the result: <html> <body> <h2>JavaScript Functions</h2> <p>This example calls a function which performs a calculation and returns the result:</p> <p id="demo"></p> <script> var x = myFunction(4, 3); document.getElementById("demo").innerHTML = x; function myFunction(a, b) { return a * b; } </script> </body> </html>

Why Functions? You can reuse code: Define the code once, and use it many times. You can use the same code many times with different arguments, to produce different results. Example: Convert Fahrenheit to Celsius: function toCelsius(fahrenheit) {     return (5/9) * (fahrenheit-32); } document.getElementById("demo").innerHTML = toCelsius(77);

Functions Used as Variable Values Functions can be used the same way as you use variables, in all types of formulas, assignments, and calculations. Example Instead of using a variable to store the return value of a function: var x = toCelsius(77); var text = "The temperature is " + x + " Celsius"; You can use the function directly, as a variable value: var text = "The temperature is " + toCelsius(77) + " Celsius";

Cont… <html> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "The temperature is " + toCelsius(77) + " Celsius"; function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } </script> </body> </html>

JavaScript Objects Real Life Objects, Properties, and Methods In real life, a car is an object. A car has properties like model, weight and color, and methods like start and stop: All cars have the same properties, but the property values differ from car to car. All cars have the same methods, but the methods are performed at different times.

JavaScript Objects You have already learned that JavaScript variables are containers for data values. This code assigns a simple value (Order) to a variable named car: var car = “Order”; Objects are variables too. But objects can contain many values. This code assigns many values (Order, 500, white) to a variable named car: var car = {type:“Order", model:"500", color:"white"}; The values are written as name:value pairs (name and value separated by a colon). JavaScript objects are containers for named values.

Object Properties The name:values pairs (in JavaScript objects) are called properties. var person = {firstName:“Ahmad", lastName:“Ali", age:50, eyeColor:"blue"}; Property Property Value firstName Ahmad lastName Ali age 50 eyeColor blue

Object Methods Methods are actions that can be performed on objects. Methods are stored in properties as function definitions. Property Property Value firstName Ahmad lastName Ali age 50 eyeColor blue fullName function() {return this.firstName + " " + this.lastName;}

Object Definition You define (and create) a JavaScript object with an object literal: Example var person = {firstName:“Ahmad", lastName:“Ali", age:50, eyeColor:"blue"}; OR var person = {     firstName:“Ahmad",     lastName:“Ali",     age:50,     eyeColor:"blue" };

Accessing Object Properties You can access object properties in two ways: objectName.propertyName OR objectName["propertyName"] Example1 person.lastName; Example2 person["lastName"];

Example <html> <body> <p> There are two different ways to access an object property: </p> <p>You can use person.property or person["property"].</p> <p id="demo"></p> <script> var person = { firstName: “Ahamd", lastName : “Ali", id : 5566 }; document.getElementById("demo").innerHTML = person["firstName"] + " " + person["lastName"]; </script> </body> </html> Output: Ahmad Ali

Accessing Object Methods You access an object method with the following syntax: objectName.methodName() Example name = person.fullName();

Example <html> <body> <p>Creating and using an object method.</p> <p>An object method is a function definition, stored as a property value.</p> <p id="demo"></p> <script> var person = { firstName: “Ahmad", lastName : “Ali", id : 5566, fullName : function() { return this.firstName + " " + this.lastName; } }; document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html>