JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,

Slides:



Advertisements
Similar presentations
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Advertisements

Introducing JavaScript
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.
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.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
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.
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,
2440: 211 Interactive Web Programming Expressions & Operators.
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
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.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Introduction to JavaScript Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan. 1.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
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.
1 JavaScript
JavaScript Syntax, how to use it in a HTML document
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Tharith Sriv.  JavaScript is a simple programming language that can be written directly into HTML documents to allow for increased interactivity with.
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.
JavaScript Jordan Clark, Jamie Kapilivsky, Taylor Monk, Jessica Torres.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
1 JavaScript in Context. Server-Side Programming.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Web Development JavaScript. Introduction to JavaScript.
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,
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.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
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.
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.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
PHP using MySQL Database for Web Development (part II)
CGS 3066: Web Programming and Design Spring 2017
Java Script Introduction. Java Script Introduction.
Introduction to Client-Side Scripting and JavaScript
Intro to JavaScript CS 1150 Spring 2017.
My First Web Page document.write("" + Date() + ""); To insert.
JavaScript Netcentric.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
PHP Introduction.
JavaScript.
JavaScript an introduction.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
PHP an introduction.
Web Programming and Design
Presentation transcript:

JavaScript

JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers, PCs, laptops, tablets, smart phones, and more.

JavaScript is a Scripting Language A scripting language is a lightweight programming language. JavaScript is programming code that can be inserted into HTML pages. JavaScript inserted into HTML pages, can be executed by all modern web browsers. JavaScript is easy to learn.

What You Will Learn Below is a taste of what you will learn in this tutorial.

JavaScript: Writing Into HTML Output document.write(" This is a heading "); document.write(" This is a paragraph ");

JavaScript: Reacting to Events Cli ck Me!

JavaScript: Changing HTML Content Using JavaScript to manipulate the content of HTML elements is a very powerful functionality. x=document.getElementById("demo") / /Find the element x.innerHTML="Hello JavaScript"; //Change the content

JavaScript: Changing HTML Images function changeImage() { element=document.getElementById('myimage') if (element.src.match("bulbon")) { element.src="pic_bulboff.gif"; } else { element.src="pic_bulbon.gif"; }

JavaScript: Changing HTML Styles Changing the style of an HTML element, is a variant of changing an HTML attribute. x=document.getElementById("demo ") //Find the element x.style.color="#ff0000"; //Change the style

JavaScript How To

JavaScripts in HTML must be inserted between and tags. JavaScripts can be put in the and in the section of an HTML page.

The Tag To insert a JavaScript into an HTML page, use the tag. The and tells where the JavaScript starts and ends. The lines between the and contain the JavaScript: alert("My First JavaScript");

The Tag You can place an unlimited number of scripts in an HTML document. Scripts can be in the or in the section of HTML, and/or in both.

External JavaScripts Scripts can also be placed in external files. External files often contain code to be used by several different web pages. External JavaScript files have the file extension.js. To use an external script, point to the.js file in the "src" attribute of the tag.

External JavaScripts

JavaScript Output

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:

Manipulating HTML Elements My First Web Page My First Paragraph document.getElementById("demo").innerHTML="My First JavaScript";

Manipulating HTML Elements The JavaScript is executed by the web browser. In this case, the browser will access the HTML element with id="demo", and replace its content (innerHTML) with "My First JavaScript".

Writing to The Document Output The example below writes a element directly into the HTML document output:

Writing to The Document Output My First Web Page document.write(" My First JavaScript ");

Warning Use document.write() only to write directly into the document output. If you execute document.write after the document has finished loading, the entire HTML page will be overwritten.

JavaScript in Windows 8 Microsoft supports JavaScript for creating Windows 8 apps. JavaScript is definitely the future for both the Internet and Windows.

JavaScript Statements

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 "Hello Dolly" inside an HTML element with id="demo":

JavaScript Statements document.getElementById("demo").innerHTML="Hello Dolly";

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. Ending statements with semicolon is optional in JavaScript.

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 HTML elements:

JavaScript Code document.getElementById("demo").innerHTML="Hello Dolly"; document.getElementById("myDIV" ).innerHTML="How are you?";

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. function myFunction() { document.getElementById("demo").inner HTML="Hello Dolly"; document.getElementById("myDIV").inne rHTML="How are you?"; }

JavaScript is Case Sensitive A function getElementById is not the same as getElementbyID. A variable named myVariable is not the same as MyVariable.

White Space 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";

Break up a Code Line You can break up a code line within a text string with a backslash. The example below will be displayed properly: document.write("Hello \ World!"); However, you cannot break up a code line like this: document.write \ ("Hello World!");

JavaScript Variables and Data Types

JavaScript Variables JavaScript variables are "containers" for storing information: var x=5; var y=6; var z=x+y;

JavaScript Variables JavaScript variables can be used to hold values (x=5) or expressions (z=x+y). Variable can have short names (like x and y) or more descriptive names (age, sum, totalvolume). Variable names must begin with a letter Variable names can also begin with $ and _ (but we will not use it) Variable names are case sensitive (y and Y are different variables)

JavaScript Data Types JavaScript variables can also hold other types of data, like text values (person="John Doe"). In JavaScript a text like "John Doe" is called a string.

JavaScript Data Types When you assign a text value to a variable, put double or single quotes around the value. When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.

Declaring (Creating) JavaScript Variables var carname; carname="Volvo"; var carname="Volvo"; var carname="Volvo"; document.getElementById("demo").inne rHTML=carname;

Note It's a good programming practice to declare all the variables you will need, in one place, at the beginning of your code.

One Statement, Many Variables var lastname="Doe", age=30, job="carpenter";

JavaScript Arithmetic As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +: y=5; x=y+2;

JavaScript Has Dynamic Types JavaScript has dynamic types. This means that the same variable can be used as different types: var x; // Now x is undefined var x = 5; // Now x is a Number var x = "John"; // Now x is a String

JavaScript Strings A string is a variable which stores a series of characters like "John Doe". A string can be any text inside quotes. You can use single or double quotes: var carname="Volvo XC60"; var carname='Volvo XC60';

JavaScript Numbers JavaScript has only one type of numbers. Numbers can be written with, or without decimals: var x1=34.00; //Written with decimals var x2=34; //Written without decimals

JavaScript Booleans var x=true; var y=false;

JavaScript Arrays var cars=new Array(); cars[0]="Saab"; cars[1]="Volvo"; cars[2]="BMW"; var cars=new Array("Saab","Volvo","BMW"); var cars=["Saab","Volvo","BMW"];

JavaScript Objects An object is delimited by curly braces. Inside the braces the object's properties are defined as name and value pairs (name : value). The properties are separated by commas: var person={firstname:"John", lastname:"Doe", id:5566};

JavaScript Objects The object (person) in the example above has 3 properties: firstname, lastname, and id. Spaces and line breaks are not important. Your declaration can span multiple lines: var person={ firstname : "John", lastname : "Doe", id : 5566 };

JavaScript Objects You can address the object properties in two ways: name=person.lastname; name=person["lastname"];

Declaring Variable Types When you declare a new variable, you can declare its type using the "new" keyword: var carname=new String; var x= new Number; var y= new Boolean; var cars= new Array; var person= new Object;

Note JavaScript variables are all objects. When you declare a variable you create a new object.

JavaScript Functions

A function is a block of code that will be executed when "someone" calls it.

JavaScript Function Syntax A function is written as a code block (inside curly { } braces), preceded by the function keyword: function functionname() { some code to be executed }

JavaScript Function Syntax The code inside the function will be executed when "someone" calls the function. The function can be called directly when an event occurs (like when a user clicks a button), and it can be called from "anywhere" by JavaScript code.

Local JavaScript Variables A variable declared (using var) within a JavaScript function becomes LOCAL and can only be accessed from within that function. (the variable has local scope). You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

Global JavaScript Variables Variables declared outside a function, become GLOBAL, and all scripts and functions on the web page can access it.

The Lifetime of JavaScript Variables The lifetime JavaScript variables starts when they are declared. Local variables are deleted when the function is completed. Global variables are deleted when you close the page.

Assigning Values to Undeclared JavaScript Variables If you assign a value to variable that has not yet been declared, the variable will automatically be declared as a GLOBAL variable.

JavaScript Arithmetic Operators

JavaScript Assignment Operators

Comparison Operators

Logical Operators

JavaScript If...Else Statements

Conditional Statements In JavaScript we have the following conditional statements: if statement - use this statement to execute some code only if a specified condition is true: if (grade>=60) { x=“pass"; }

Conditional Statements In JavaScript we have the following conditional statements: if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false: if (grade>=60) { x=“pass"; } else { x=“fail”; }

Conditional Statements In JavaScript we have the following conditional statements: if...else if....else statement - use this statement to select one of many blocks of code to be executed: if (grade>=90) { x=“pass with A grade"; } else if (grade>=60) { x=“pass"; } else { x=“fail"; }

Conditional Statements In JavaScript we have the following conditional statements: switch statement - use this statement to select one of many blocks of code to be executed

JavaScript Loops

Different Kinds of Loops JavaScript supports different kinds of loops: for - loops through a block of code a number of times: for (var i=0; i "; }

Different Kinds of Loops JavaScript supports different kinds of loops: while - loops through a block of code while a specified condition is true: while (i "; i++; }

Note If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.

Different Kinds of Loops JavaScript supports different kinds of loops: do/while - also loops through a block of code while a specified condition is true. The loop will always be executed at least once: do { x=x + "The number is " + i + " "; i++; } while (i<5);

Comparing For and While cars=["BMW","Volvo","Saab","For d"]; var i=0; for (;cars[i];) { document.write(cars[i] + " "); i++; }

Comparing For and While cars=["BMW","Volvo","Saab","For d"]; var i=0; while (cars[i]) { document.write(cars[i] + " "); i++; }