>> JavaScript: Location, Functions and Objects

Slides:



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

2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Javascript and the Web Whys and Hows of Javascript.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
>> Introduction to JavaScript
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.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
JavaScript Syntax, how to use it in a HTML document
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
JavaScript, Fourth Edition
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
1 Server versus Client-Side Programming Server-SideClient-Side.
CHAPTER 3 FUNCTIONS, METHODS & OBJECTS WHAT IS A FUNCTION?
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,
Functions Function is a standalone block of statements that performs some tasks and returns a value. Functions must be declared before they can be used.
Understanding JavaScript and Coding Essentials Lesson 8.
JavaScript Arrays, Functions, and Forms George Mason University.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
Java Script Programming. Review: Event Handling Text Box Title: Button.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
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.
Tarik Booker CS 120 California State University, Los Angeles.
CGS 3066: Web Programming and Design Spring 2017
Java Script Introduction. Java Script Introduction.
>> Introduction to JavaScript
Chapter 6 JavaScript: Introduction to Scripting
HTML & teh internets.
JavaScript is a programming language designed for Web pages.
Introduction to JavaScript Events
JAVA Script : Functions Ashima Wadhwa
Basics of JavaScript Programming Language
JavaScript Functions.
Exploring JavaScript Ch 14
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
JavaScript Introduction
DHTML Javascript Internet Technology.
Understanding JavaScript and Coding Essentials
Your 1st Programming Assignment
Javascript: variables and parameters
Event Driven Programming & User Defined Functions
JavaScript – Part I Mendelsohn.
DHTML Javascript Internet Technology.
The Web Wizard’s Guide To JavaScript
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
CHAPTER 3 FUNCTIONS, METHODS & OBJECTS
JavaScript is a scripting language designed for Web pages by Netscape.
Introduction to Programming and JavaScript
Web Programming and Design
Web Programming and Design
[Robert W. Sebesta, “Programming the World Wide Web
Presentation transcript:

>> JavaScript: Location, Functions and Objects

JavaScript Location in html

Web-based Systems | Misbhauddin JavaScript Location <input type=button” onclick="alert(‘Hello');“/> Inline <script type=“text/javascript”> //Code goes here </script> Internal jsfile.js <script type=“text/javascript” src=“jsfile.js”></script> External Web-based Systems | Misbhauddin

JavaScript Location – Inline <button onclick=“alert(‘Welcome’);" >Click Here</button> Event (can be other events too like onblur ……) Note: Cannot write longer JS statements / complete code Web-based Systems | Misbhauddin

JavaScript Location – Internal Optional in HTML5 <script type=“text/javascript”> alert(‘Welcome’); </script> Web-based Systems | Misbhauddin

JavaScript Location inside HTML <head> <title>JavaScript Location</title> </head> <body> </body> </html> <script type=“text/javascript”> </script> In the Head Functions are loaded before the buttons, links or other things that call them are loaded <script type=“text/javascript”> </script> In the Body Functions that needs running after the whole page (body) of the HTML is loaded Web-based Systems | Misbhauddin

JavaScript Location – External script.js function test() { alert(‘Hello’); } Inside the head or the body tag <html> <head> <script src=“script.js”></script> </head> <body> </body> </html> Web-based Systems | Misbhauddin

FUNCTIONS in JS

Web-based Systems | Misbhauddin Function Declaration Mainly used for event-handling in Web Development Can also be called from other functions (Reuse) You can use as many parameters as you like Can also return values (numbers, strings, Boolean) Use return statement SYNTAX keyword function name(param1, param2,…..) { } function name(parameters) { return b; } Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin Functions in JS Named Functions Anonymous Functions Named Functions: Functions that are given a name so that we can call them later in the code. function area( width, height) { return width* height; } var size = area(3, 4); Function declaration Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin Anonymous Functions Typically, after an = sign, the JavaScript interpreter expects to see an expression such as var sum = 1+2; expression If there is a function declared after the equal sign, this is called a function expression. In other words, it is function stored in a variable. Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin Anonymous Functions var area = function( width, height) { return width* height; } var size = area(3, 4); Anonymous functions – functions created using the expression These are functions with no name Used mainly in calls and event handling Used to create immediately invoked expressions (next slide) Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin Anonymous Functions An important use of anonymous function Immediately Invoked Function Expression (IIFE) Pronounced “iffy” They are executed once var area = (function( width, height) { return width* height; } () ); Grouping parenthesis – to treat it as an expression Final parenthesis – to call the function immediately Web-based Systems | Misbhauddin

Event Handling in JavaScript Using onclick function Using the addEventListener() function button.onclick = function(){ } button.addEventListener(“click”, function(){ }); Note: Using addEventListener(), you can add as many listeners as possible. But this function has limited IE support Web-based Systems | Misbhauddin

Objects in JS

Web-based Systems | Misbhauddin Object-Oriented JS JavaScript is Object-Oriented Properties  They have a value Object Methods 'things that do something Example var abc = “Hello World”; abc.length; document.write(“Hello World”); method property Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin Object Definition Objects Objects allow us to represent in code real world things and entities Such as a person or bank account SYNTAX keyword var object-name = { ………. } Web-based Systems | Misbhauddin

Key-Value: Properties of an Object Each piece of information we include in an object is known as a property.  Each property has a key, followed by : and then the value of that property SYNTAX Access value of a key keyword object-name[“key”]; or object-name.key var object-name = { key: value, } Note: Separate each property using a comma (,) and not a semicolon as in Java Key names can have quotations. But if there is a space in the name (first name), quotation is necessary Web-based Systems | Misbhauddin

Function Assignment to Objects Methods A method is just like a function associated with an object SYNTAX keyword var object-name = { key: value, function-name: function() { ----------- } Access the function object-name.function-name(); Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin this Keyword This keyword Used to refer to the current object inside the function in the object var hotel = { rooms: 50, booked: 30, checkAvailability: function() { return this.rooms – this.booked; } Web-based Systems | Misbhauddin

Web-based Systems | Misbhauddin Exercise Create and Initialize a JavaScript Object with the following details Function called “updateGPA” that should take one parameter and update the GPA of mohammed to the new GPA from the given parameter. KEY VALUE Name Mohammed GPA 3.75 Web-based Systems | Misbhauddin