<html> <body> <h1>My First Web Page</h1> <script type="text/javascript"> document.write("<p>" + Date() + "</p>"); </script> </body> </html> To insert.

Slides:



Advertisements
Similar presentations
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
Advertisements

14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
HTML Forms JavaScript Introduction / Overview Lab Homework #1 CS 183 4/8/2010.
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I Javascript Jongwook Woo,
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1 تدریس طراحی وب برای اطلاعات بیشتر تماس بگیرید تاو شماره تماس: پست الکترونیک :
Adding JavaScript (<script tag>)
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro 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.
1 JavaScript 1. 2 Java vs. JavaScript Java –Is a high level programming language –Is platform independent –Runs on a standardized virtual machine.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
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.
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.
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.
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.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
JavaScript Lectures Level 7. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting.
Web Development JavaScript. Introduction to JavaScript.
Chapter 5: Intro to Scripting CIS 275—Web Application Development for Business I.
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.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Pertemuan 3 JavaScript.
Java Script Introduction. Java Script Introduction.
>> Introduction to JavaScript
VB Script V B S.
Introduction to Client-Side Scripting and JavaScript
Chapter 6 JavaScript: Introduction to Scripting
JavaScript is a programming language designed for Web pages.
PHP 5 Syntax.
Exploring JavaScript Ch 14
WEB APPLICATION PROGRAMMING
Introduction to Scripting
JavaScript Syntax and Semantics
JavaScript Netcentric.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
PHP Introduction.
JavaScript.
Chapter 19 JavaScript.
Introduction to Client-Side Scripting and JavaScript
Pertemuan 3 JavaScript.
PHP.
My First Web Page document.write("" + Date() + ""); To insert.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
CS105 Introduction to Computer Concepts
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
Web Programming– UFCFB Lecture 13
CIS 136 Building Mobile Apps
Programming Basics Review
Web Programming and Design
CS105 Introduction to Computer Concepts JavaScript
Presentation transcript:

<html> <body> <h1>My First Web Page</h1> <script type="text/javascript"> document.write("<p>" + Date() + "</p>"); </script> </body> </html> To insert a JavaScript into an HTML page, use the <script> tag. Inside the <script> tag use the type attribute to define the scripting language. The <script> and </script> tells where the JavaScript starts and ends:

<html> <body> <h1>My First Web Page</h1> <p id="demo">This is a paragraph.</p> <script type="text/javascript"> document.getElementById("demo").innerHTML=Date(); </script> </body> </html> Without the <script> tag(s), the browser will treat "document.getElementById("demo").innerHTML=Date();" as pure text and just write it to the page

<html> <body> <script type="text/javascript"> < <html> <body> <script type="text/javascript"> <!-- document.getElementById("demo").innerHTML=Date(); //--> </script> </body> </html> The two forward slashes at the end of comment line (//) is the JavaScript comment symbol. This prevents JavaScript from executing the --> tag.

JavaScripts can be put in the <body> and in the <head> sections of an HTML page The example below writes the current date into an existing <p> element when the page loads <html> <body><h1>My First Web Page</h1> <p id="demo"></p> <script type="text/javascript"> document.getElementById("demo").innerHTML=Date(); </script> </body> </html> Note that the JavaScript is placed at the bottom of the page to make sure it is not executed before the <p> element is created.

JavaScript Functions and Events The example below calls a function when a button is clicked <html> <head> <script type="text/javascript"> function displayDate() { document.getElementById("demo").innerHTML=Date(); } </script> </head> <body> <h1>My First Web Page</h1> <p id="demo"></p> <button type="button" onclick="displayDate()">Display Date</button> </body> </html>

You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section at the same time. It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content JavaScript is Case Sensitive Unlike HTML, JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions. add a semicolon at the end of each executable statement. Most people think this is a good programming practice. The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement.

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. This example will write a heading and two paragraphs to a web page:

JavaScript Comments Comments can be added to explain the JavaScript, or to make the code more readable. single line comments start with //. <script type="text/javascript"> // Write a heading document.write("<h1>This is a heading</h1>"); // Write two paragraphs: document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script>

JavaScript Multi-Line Comments Multi line comments start with /* and end with */. <script type="text/javascript"> /* The code below will write one heading and two paragraphs */ document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script>

Use comment at the end of line <script type="text/javascript"> document.write("Hello"); // Write "Hello" document.write(" Dolly!"); // Write " Dolly!" </script> Use comment to prevent from execution <script type="text/javascript"> //document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script>

Variables variables can be used to hold values (x=5) or expressions (z=x+y). A variable can have a short name, like x, or a more descriptive name, like carname. Rules for JavaScript variable names: Variable names are case sensitive (y and Y are two different variables) Variable names must begin with a letter or the underscore character You declare JavaScript variables with the var keyword. When you assign a text value to a variable, use quotes around the value. Example: var x; var carname;

<html> <body> <script type="text/javascript"> var firstname; firstname="Hege"; document.write(firstname); document.write("<br />"); firstname="Tove"; </script> <p>The script above declares a variable, assigns a value to it, displays the value, changes the value, and displays the value again.</p> </body> </html> A variable's value can change during the execution of a script. You can refer to a variable by its name to display or change its value

Variables declared outside a function become GLOBAL, and all scripts and functions on the web page can access it. Global variables are destroyed when you close the page. If you declare a variable, without using "var", the variable always becomes GLOBAL. x=10; carname=“Toyota"; JavaScript Arithmetic As with algebra, you can do arithmetic operations with JavaScript variables y=x-5; z=y+5; = is used to assign values. + is used to add values.

JavaScript Arithmetic Operators Arithmetic operators are used to perform arithmetic between variables and/or values. Given that y=5, the table below explains the arithmetic operators: Given y=5; z=2; x=y+z;

The + Operator Used on Strings The + operator can also be used to add string variables or text values together. To add two or more string variables together, use the + operator. txt1=“May I know txt2=“your name"; txt3=txt1+txt2; or insert a space into the expression txt1=“May I know txt2=“your name"; txt3=txt1+ “ “+txt2;

<html> <body> <script type="text/javascript"> var x; x=5+5; document.write(x); document.write("<br />"); x="5"+"5"; x=5+"5"; x="5"+5; </script> <p>The rule is: If you add a number and a string, the result will be a string.</p> </body> </html>

JavaScript Comparison and Logical Operators Comparison Operators Comparison operators are used in logical statements to determine equality or difference between variables or values.

Logical Operators Logical operators are used to determine the logic between variables or values. Given that x=6 and y=3, the table below explains the logical operators Operator Description Example && and (x < 10 && y > 1) is true || or (x==5 || y==5) is false ! not !(x==y) is true

JavaScript If...Else Statements Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. 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...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

If statement Syntax: if (condition)   {   code to be executed if condition is true   } <script type="text/javascript"> //Write a "Good morning" greeting if //the time is less than 10 var d=new Date(); var time=d.getHours(); if (time<10)   {   document.write("<b>Good morning</b>");   } </script>

If...else Statement Use the if....else statement to execute some code if a condition is true and another code if the condition is not true. Syntax: if (condition)   {   code to be executed if condition is true   } else   {   code to be executed if condition is not true   }

This example demonstrates the If...Else statement. </p> <html> <body> <script type="text/javascript"> var d = new Date(); var time = 10 //d.getHours(); if (time < 10) { document.write("<b>Good morning</b>"); } else document.write("<b>Good day</b>"); </script> // continuation <p> This example demonstrates the If...Else statement. </p> If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting. </body> </html>

If...else if...else Statement Use the if....else if...else statement to select one of several blocks of code to be executed. Syntax if (condition1)   {   code to be executed if condition1 is true   } else if (condition2)   {   code to be executed if condition2 is true   } else   {   code to be executed if neither condition1 nor condition2 is true   }

Examples: <script type="text/javascript"> var d = new Date() var time = d.getHours() if (time<10)   {   document.write("<b>Good morning</b>");   } else if (time>=10 && time<16)   {   document.write("<b>Good day</b>");   } else   {   document.write("<b>Hello World!</b>");   } </script>

JavaScript Switch Statement The JavaScript Switch Statement Use the switch statement to select one of many blocks of code to be executed. Syntax switch(n) { case 1:   execute code block 1   break; case 2:   execute code block 2   break; default:   code to be executed if n is different from case 1 and 2 }

<script type="text/javascript"> //You will receive a different greeting based //on what day it is. Note that Sunday=0, //Monday=1, Tuesday=2, etc. var d=new Date(); var theDay=d.getDay(); switch (theDay) { case 5:   document.write("Finally Friday");   break; case 6:   document.write("Super Saturday");   break; case 0:   document.write("Sleepy Sunday");   break; default:   document.write("I'm looking forward to this weekend!"); } </script> Use break to prevent the code from running into the next case automatically.

<script type="text/javascript"> var x=5; switch (x) { case x=0: document.write("<b>Your score is zero</b>"); break; case x=1: document.write("<b>You’ve got 1 point</b>"); case x=2: document.write("<b>That’s 2 points</b>"); default: document.write("<b>That’s awesome</b>"); } </script>

1.Write a javascript program that will display a stanza with four lines. 2.Write a program that will hold the value 5 for x and Toyota for carname. 3.Write a program that will combine your last name ,firstname and middle name 4