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.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
The Web Warrior Guide to Web Design Technologies
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
Javascript and the Web Whys and Hows of Javascript.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
JavaScript 1. What is JavaScript? JavaScript allows web authors to create dynamic pages that react to user interaction. It is an Object-based because.
Week 9 PHP Cookies and Session Introduction to JavaScript.
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.
Introduction to JavaScript Gordon Tian
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
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.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
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.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
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.
LOGO Introduction to Client-Side Scripting and JavaScript CHAPTER 9 Eastern Mediterranean University School of Computing and Technology Department of Information.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax, how to use it in a HTML document
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Server versus Client-Side Programming Server-SideClient-Side.
Introduction into JavaScript Java 1 JavaScript JavaScript programs run from within an HTML document The statements that make up a program in an HTML.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
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,
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
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.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Module 1 Introduction to JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Tutorial 10 Programming with JavaScript
Intro to JavaScript CS 1150 Spring 2017.
My First Web Page document.write("" + Date() + ""); To insert.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
Intro to PHP & Variables
JavaScript an introduction.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Tutorial 10 Programming with JavaScript
Tutorial 10: Programming with javascript
Presentation transcript:

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 of Information TechnologyT2-Lecture-6x

JavaScript Part - I For Lecture Material/Slides Thanks to:

Synopsis Introduction JavaScript Where To JavaScript Output JavaScript Syntax JavaScript Statements JavaScript Comments JavaScript Variables JavaScript Data Type 3 T2-Lecture-6 Ahmed Mumtaz Mustehsan

Introduction

Introduction JavaScript is the programming language of the Web. All modern HTML pages are using JavaScript. JavaScript is one of 3 languages that all web developers MUST learn: HTML to define the content of web pages CSS to specify the layout of web pages JavaScript to program the behavior of web pages JavaScript is the most popular programming language in the world. It is the language for HTML, for the Web, for computers, servers, laptops, tablets and smart phones. 5 T2-Lecture-6 Ahmed Mumtaz Mustehsan

Where To place JavaScript Where To place JavaScript In HTML, Java Scripts must be inserted between and tags. The lines between and contain the JavaScript code: Java Scripts can be put in the and in the section of an HTML page. 6 T2-Lecture-6 Ahmed Mumtaz Mustehsan

Example function myFunction() { document.getElementById("demo").innerHTML = "My First JavaScript Function"; } Just take it for a fact, that the browser will interpret the code between the and tags as JavaScript. Old examples may have type="text/javascript" in the tag. This is no longer required. JavaScript is the default scripting language in all modern browsers and in HTML5. T2-Lecture-6 Ahmed Mumtaz Mustehsan 1-7

JavaScript Functions and Events Often, JavaScript code is written to be executed when an event occurs, like when the user clicks a button. JavaScript code inside a function, can be invoked later, when an event occurs. Invoke a function = Call upon a function (ask for the code in the function to be executed). T2-Lecture-6 Ahmed Mumtaz Mustehsan 1-8

JavaScript in or JavaScript in or You can place any number of scripts in an HTML document. Scripts can be placed in the or in the section of HTML, and/or in both. Scripts may also be placed at the bottom of the section of a web page. This can reduce display time. Sometimes you will see all JavaScript functions in the section. Separating HTML and JavaScript, by putting all the code in one place, is always a good habit. T2-Lecture-6 Ahmed Mumtaz Mustehsan 1-9

JavaScript in JavaScript in In this example, a JavaScript function is placed in the section of an HTML page. The function is invoked (called) when a button is clicked: function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed.“;} My Web Page A Paragraph Try it Demo!!! T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript in JavaScript in In this example, a JavaScript function is placed in the section of an HTML page. The function is invoked (called) when a button is clicked: My Web Page A Paragraph Try it function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } ◦ Demo2!!! Demo2!!! T2-Lecture-6 Ahmed Mumtaz Mustehsan

External JavaScripts Scripts can also be placed in external files. External scripts are practical when the same code is used in many different web pages. External JavaScript files have the file extension.js. To use an external script, put the name of the script file in the source (src) attribute of the tag: ◦ T2-Lecture-6 Ahmed Mumtaz Mustehsan

External JavaScripts… You can place an external script reference in or as you like. The script will behave as if it was located exactly where you put the reference in the HTML document. External scripts cannot contain tags. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Output

JavaScript does not have any print or output functions. In HTML, JavaScript can only be used to manipulate HTML elements. T2-Lecture-6 Ahmed Mumtaz Mustehsan

Manipulating HTML Elements To access an HTML element from JavaScript, you can use the document.getElementById(id) method. Use the "id" attribute to identify the HTML element, and innerHTML to refer to the element content: My First Web Page My First Paragraph document.getElementById("demo").innerHTML = "Paragraph changed."; T2-Lecture-6 Ahmed Mumtaz Mustehsan

Manipulating HTML Elements… The JavaScript statement (inside the tag) is executed by the web browser: document.getElementById("demo") is JavaScript code for finding an HTML element using the id attribute. innerHTML = "Paragraph changed." is JavaScript code for changing an element's HTML content (innerHTML). T2-Lecture-6 Ahmed Mumtaz Mustehsan

Writing to The HTML Document For testing purposes, you can use JavaScript to write directly to the HTML document: ◦ My First Web Page My first paragraph. document.write(Date()); ◦ Demo-write!! Demo-write!! T2-Lecture-6 Ahmed Mumtaz Mustehsan

Writing to The HTML Document… Use document.write for testing only. If you execute it, on a loaded HTML document, all HTML elements will be overwritten. ◦ My First Web Page My first paragraph. Try it function myFunction() { document.write(Date()); } T2-Lecture-6 Ahmed Mumtaz Mustehsan

Writing to The Console If your browser supports debugging, you can use the console.log() method to display JavaScript values in the browser. Activate debugging in your browser with F12, and select "Console" in the debugger menu. My First Web Page a = 5; b = 6; c = a + b; console.log(c); T2-Lecture-6 Ahmed Mumtaz Mustehsan

Writing to The Console Debugging is the process of testing, finding, and reducing bugs (errors) in computer programs. The first known computer bug was a real bug (an insect), stuck in the electronics. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Syntax

JavaScript Syntax JavaScript is a programming language. The Syntax rules define how the language is constructed. JavaScript is a scripting language. It is a lightweight, but powerful, programming language. Syntax : Syntax : "The principles by which sentences are constructed in a language." The sentences of a programming language are called computer statements, or just statements. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Literals In a programming language, literals are constant values like Number literals can be written with or without decimals, and with or without scientific notation (e): ◦ e5 String literals can be written with double or single quotes: ◦ "John Doe" 'John Doe' T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Variables In a programming language, variables are containers for storing information (data). The equal sign (=) assigns a value to a named variable (just like in normal algebra):  x = 5 length = 6 T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Operators JavaScript uses operators to compute values (just like algebra):  a * b JavaScript can assign computed values to named variables (just like algebra):  x = y = x * 10 Expressions like 5 + 6, and x * 10, are called expression literals. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Statements In HTML, JavaScript statements are written as sequences of "commands" to the HTML browser. Statements are separated by semicolons:  x = ; y = x * 10 ; T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Keywords A JavaScript statement often starts with a keyword. The var keyword tells the browser to create a new variable: var x = 5 + 6; var y = x * 10; T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Comments Not all JavaScript statements are "commands". Anything after double slashes // is ignored by the browser: considered as comments for self documentation. // I will not be executed Comments will be discussed later! T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Data Types JavaScript variables can hold many types of data: numbers, text strings, arrays, objects and much more: var length = 16; // Number assigned by a number literal var lastName = "Johnson"; // String assigned by a string literal var cars = [“Toyota", “Honda", "BMW"]; // Array assigned by an array literal // Object assigned by an object literal var person = {firstName:John, lastName:Doe}; T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Functions JavaScript statements written inside a function, can be invoked many times (reused): Invoke a function = Call upon a function (ask for the code in the function to be executed). function myFunction(a, b) { return a * b; // returns the product of a and b } T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Identifiers All programming languages must identify variables, functions, and objects, with unique names. These unique names are called identifiers. Identifier names can contain letters, digits, underscores, and dollar signs, but cannot begin with a number. Reserved words (like JavaScript keywords) cannot be used as identifiers. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript is Case Sensitive In JavaScript all identifiers are case sensitive. The variables lastName and lastname, are two different variables. The functions myFunction and myfunction, are two different functions. JavaScript does not interpret Var; as var. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Character Set JavaScript uses the Unicode character set. Unicode covers (almost) all the characters, punctuations, and symbols in the world. It is common, in JavaScript, to use camelCase names. You will often see identifier names written like lastName (instead of lastname). T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Statements

In HTML, JavaScript statements are "commands" to the browser. The purpose, of the statements, is to tell the browser what to do. This JavaScript statement tells the browser to write " WelCome to Java Script " inside an HTML element identified with id="demo": Example  document.getElementById("demo").innerHTML = “WelCome to Java Script."; T2-Lecture-6 Ahmed Mumtaz Mustehsan

Semicolon ; Semicolon separates JavaScript statements. Normally you add a semicolon at the end of each executable statement. Using semicolons also makes it possible to write many statements on one line. Writing: a = 5; b = 6; c = a + b; Is the same as writing: a = 5; b = 6; c = a + b; You might see examples without semicolons. Ending statements with semicolon is optional in JavaScript. T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Code JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. This example will manipulate two different HTML elements: Example  document.getElementById("demo").innerHTML = " WelCome to Java Script."; document.getElementById("myDiv").innerHTML = "How are you?"; T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Code Blocks JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket, and end with a right curly bracket. The purpose of a block is to make the sequence of statements execute together. A good example of statements grouped together in blocks, are in JavaScript functions. This example will run a function that will manipulate two HTML elements: Example function myFunction() { document.getElementById("demo").innerHTML = “Welcome to Java Script."; document.getElementById("myDIV").innerHTML = "How are you?"; } T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Statement Identifiers JavaScript statements often start with a statement identifier to identify the JavaScript action to be performed. Statement identifiers are reserved words and cannot be used as variable names (or any other things). Here is a list of some of the JavaScript statements (reserved words) T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript Statement Identifiers… Here is a list of some of the JavaScript statements (reserved words) you will learn about in this tutorial: T2-Lecture-6 Ahmed Mumtaz Mustehsan

JavaScript White Space in JavaScript Statements JavaScript ignores extra spaces. You can add white space to your script to make it more readable. The following lines are equivalent: ◦ var person="Hege" var person = "Hege" T2-Lecture-6 Ahmed Mumtaz Mustehsan

Breaking a JavaScript Statement You can break up a code line within a text string with a backslash: ◦ var text = "Hello \ World!"; However, you cannot break up a code line like this: ◦ var text = \ "Hello World!"; T2-Lecture-6 Ahmed Mumtaz Mustehsan

JS Comments

JavaScript Comments JavaScript comments can be used to explain the code, and make the code more readable. JavaScript comments can also be used to prevent execution, when testing alternative code. T2-Lecture-7 Ahmed Mumtaz Mustehsan

Single Line Comments Single line comments start with //. Any text between // and the end of a line, will be ignored by JavaScript (will not be executed). The following example uses a single line comment in front of each line, to explain the code: T2-Lecture-7 Ahmed Mumtaz Mustehsan

Example // Change heading: document.getElementById("myH").innerHTML = "My First Page"; // Change paragraph: document.getElementById("myP").innerHTML = "My first paragraph."; This example uses a single line comment at the end of each line, to explain the code: var x = 5; // Declare x, give it the value of 5 var y = x + 2; // Declare y, give it the value of x + 2 T2-Lecture-7 Ahmed Mumtaz Mustehsan

Multi-line Comments Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by JavaScript. The following example uses a multi-line comment (a comment block) to explain the code: T2-Lecture-7 Ahmed Mumtaz Mustehsan

Using Comments to Prevent Execution Using comments to prevent execution of code, can be very suitable for testing. Adding // in front of a code line changes the code lines from an executable line to a comment. The next example uses // to prevent execution of one of the code lines. //document.getElementById("myH").innerHTML = "My First Page"; document.getElementById("myP").innerHTML = "My first paragraph."; T2-Lecture-7 Ahmed Mumtaz Mustehsan

Example /* The code below will change the heading with id = "myH" and the paragraph with id = "myp" in my web page: */ document.getElementById("myH").innerHTML = "My First Page"; document.getElementById("myP").innerHTML = "My first paragraph."; T2-Lecture-7 Ahmed Mumtaz Mustehsan

Example The following example uses a comment block to prevent execution of multiple lines: /* document.getElementById("myH").innerHTML = "My First Page"; document.getElementById("myP").innerHTML = "My first paragraph."; */ T2-Lecture-7 Ahmed Mumtaz Mustehsan

The End JavaScript Part-I Thank You T2-Lecture-6 Ahmed Mumtaz Mustehsan