javascript programming language. introduction javascript is a scripting language. the term scripting...

44
JAVASCRIPT JAVASCRIPT PROGRAMMING LANGUAGE PROGRAMMING LANGUAGE

Upload: dwight-gregory

Post on 25-Dec-2015

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

JAVASCRIPT JAVASCRIPT PROGRAMMING PROGRAMMING

LANGUAGELANGUAGE

Page 2: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

IntroductionIntroduction

JavaScript is a scripting language. The term scripting language refers to programming languages that are executed by an interpreter from within a Web browser. An interpreter translates programming code into an executable format each time the program is run – one line at a time. Programs written in scripting languages, such as JavaScript, are interpreted when a scripting engine loads an HTML page.

Page 3: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

The JavaScript language was first introduced in Navigator and was originally called LiveScript.

With the release of Navigator 2.0, the name was changed to JavaScript 1.0. Subsequently, Microsoft released its own version of JavaScript in Internet Explorer 4.0 and named it Jscript.

Introduction (Continued)

Page 4: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

JavaScript’s Role on the WebJavaScript’s Role on the Web

JavaScript brings HTML to life and makes Web pages dynamic. Instead of HTML documents being static, JavaScript can turn them into applications, such as games or order forms. You can use JavaScript to change the contents of a Web page after it has been rendered by a browser, to interact with a user through forms and controls, to create visual effects such as animation, and to control the web browser window itself. None of these things was possible before the creation of JavaScript.

Page 5: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

JavaScript is available in two formats: Client-side JavaScript and Server-side JavaScript. The standardized client-side JavaScript is the format available to HTML pages displayed in Web browsers (the client). JavaScript version 1.2 in Navigator 4.0 and ECMScript are client-side versions of JavaScript. Server-side JavaScript is used with Web servers to access file systems, communicate with other applications, access databases, and perform other tasks.

JavaScript’s Role on the JavaScript’s Role on the WebWeb

Page 6: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Script BeginningScript Beginning

JavaScript Programs run from within an HTML document. The statements that make up a JavaScript program in an HTML document are contained between the <SCRIPT> ….. </SCRIPT> tag pairs. The <SCRIPT> tag is used to notify the Web Browser that the commands that follow it need to be interpreted by a scripting engine. The LANGUAGE attribute of the <SCRIPT> tag tells the browser which scripting language and which version of the scripting language is being used.

<SCRIPT LANGUAGE=“JAVASCRIPT”>

JAVASCRIPT STATEMENTS;

</SCRIPT>

Page 7: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

First JavaScript ProgramFirst JavaScript Program

<PRE>

<Script Language=“JavaScript1.4”

Document.writeln(“Hello World”>; //Print on the screen

Document.write(“This line is printed below the ‘Hello World’ line.”);

</Script>

</PRE>

[Note: The write() and writeln() methos of the document object require a text string as an argument.]

Page 8: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Structure of JavaScript Programming

<HTML>

<HEAD>

<TITILE> … </TITLE>

<Script Language=“JavaScript”>

Define any function

</Script>

<BODY>

<Script Language=“JavaScript”>

Use defined function

</script>

</HEAD>

</HTML>

Should be :

“javascript” OR

JavaScript

Page 9: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML><HEAD><TITLE> Multiple JavaScript Calls </Title></Head/><Body> <Script Language="JavaScript" SRC="source.js"></Script><pre> <script language="JavaScript"> document.writeln("This line was created with embedded JavaScript code."); document.writeln("This line was also created with embedded JavaScript code.");</Script></pre></body><HTML>

Internal and External Source for print

document.write("this line was printed from the JavaScript spurce file.")

Save the file as JavaScriptsource.js

Page 10: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Adding Comments to a JavaScript Program

<Script Language=“JavaScript1.2”>

/*

This line is part of the block comment.

This line is also part of the block comment

*/

Document.writeln(“somments Example”); //Line comments con follow code statements

//This line comment takes up an entire line.

/*This is another way of creating a block comment.*/

</Script>

Page 11: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Hiding JavaScript from Incompatible Browsers<HTML>

<HEAD>

<TITLE></TITLE>

</HEAD>

<BODY>

This line is rendered normally since it is located before the opening comment tag. <BR>

<!- - text on this line is not displayed

Text on this line is not displayed

This line is not displayed either -->

This line is rendered normally since it is located after the closing comment tag. <BR>

</BODY>

</HTML>

Page 12: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML><HEAD><TITLE> Multiple JavaScript Calls </Title>

<Script Language="JavaScript"<!– Begin Hiding JavaScript mylink= "C:\Documents and Settings\Administrator\Desktop\1.html"</Script><Body><a href= "&{mylink};"> click here </A></body><HTML>

Page 13: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Variables, Functions, Variables, Functions, Objects, and EventsObjects, and Events

Page 14: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

VariableVariableOne of the most important aspects of programming is the ability to store and manipulate values in computer memory location. The values stored in computer memory locations are called variables.

In JavaScript, you use the reserved keyword Var to create variable. Reserved word, or keywords, are part of the JavaScript language syntax. Reserved words cannot be used for variable names.

Abstract Char Do Finally Boolean Class

Double Float Break Const Else For

Byte Continue Extends Function Case Default

False Goto Catch Delete Final If

Implements New Static True Import Null

Super Try Import Null Super Try

In Package Switch Typeof Instanceof Public

Throw While Long Return Throws With

Native Short Transient

Page 15: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

The value you assign a variable can be a literal string or a numeric value.

Var myvariable=“Hello”; ------- String

Var myvariable=100; ------------ numeric

You can declare multiple variables in the same statement using a single Var keyword:

Var firstvar=“text” , secondvar=100, thirdvar=2.5;

Ex.

Var myDog=“Golden Retriever”;

Document.writeln(myDog);

myDog=“Irish Setter”;

document.writeln(myDog);

Page 16: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML><HEAD> <SCRIPT language = JavaScript> var name = prompt("Enter your name", "Name"); </SCRIPT></HEAD>

<BODY> <SCRIPT language = "JavaScript"> document.write("<H2> Hello " + name + "</H2>"); </SCRIPT></BODY></HTML>

USER INPUT

Page 17: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

To Display String and Number<HTML><HEAD><TITLE>Script</TITLE><SCRIPT LANGUAGE=“JavaScript”>document.write("JavaScript")</script></HEAD>

<body><SCRIPT LANGUAGE="JavaScript">document.write("Hello everybody")document.write(1000)</Script></BODY></HTML>

Page 18: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Simple Convert on Hexadecimal Value<HTML><HEAD><TITLE>Integer</TITLE>Integral Value with String</HEAD>

<body><SCRIPT LANGUAGE="JavaScript">document.write(00123 + "<br>")document.write(2*125 + "<br>" + 123 + "Aptech")</Script></BODY></HTML>

Page 19: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Hexadecimal to Binary

<HTML><HEAD><TITLE>Hexadecimal Code</TITLE>This Hexadecimal Code convert to Binary.

</HEAD>

<body><SCRIPT LANGUAGE="JavaScript">document.write("<br>" + "<Font color=red face=Arial size=5>")document.write("Notebook" + 10e5)document.write("<center>" + "Good Morning" + "</center>" + 10e-5)</Script></BODY></HTML>

Page 20: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Print Number<html><head><title> Print Numbers </Title></head><body><pre><Script Language="JavaScript"> var integervar = 150; var floatingpointvar = 3.0e7; document.writeln(integervar); document.writeln(floatingpointvar);</script></pre></body></html>

Page 21: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Variable Calculation<HTML><HEAD> Variable </HEAD><title> Variable </title><BODY bgcolor="Red"> <Script Language="JavaScript">

var a = "Ram has scored";var b = 100;var c = "in Final";var d = a+b+c;var e = 100;document.writeln("<font color=Blue face=Arial size=4>");document.writeln("<br>" + a + b );document.writeln("<br>" + d + "<br>" + "<hr>");document.writeln("Total" + c + "is" + b + e + "</font>");</script></body></HTML>

Page 22: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Variable Calculation

<HTML><HEAD> Variable calculation</HEAD><title> Variable </title><BODY bgcolor=wheat> <Script Language="JavaScript">var a = 100;var b = -109;var c = 15;var d = a*c;var e = a/c;

Page 23: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

document.writeln("<hr color=Blue size=5>" + "<Font color=Blue size=4>");document.writeln("a+b" + "=" + (a+b));document.writeln("<br>" + "a-b" + "=" + (a-b));document.writeln("<br>" + "(a+b-e)" + "=" + (a+b-e));document.writeln("<br>" + "(a*b-d)" + "=" + (a*b-d));document.writeln("<br>" + "(a/b*c)" + "=" + (a/b*c));document.writeln("</font>");</script></body></HTML>

Page 24: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Variable Calculation<script language="javascript">var name="anil";var salary=900;var da=90;var hra=90;var totalsalary=salary+da+hra;document.write("<font size=7 color=red><b><i>"+"name is" +name+"<br>");document.write("<font size=7 color=red><b><i>"+"da is"+da +"<br>");document.write("<font size=7 color=red><b><i>"+"hra is" +hra+"<br>");document.write("<font size=7 color=red><b><i>"+"total salary is" + totalsalary+"<br>");</script>

Page 25: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Define Global Variable<HTML><HEAD> <title> Variable scope </title><Script Language="JavaScript">var First_Global_Variable = "First Global Variable";function Scope_Example() { Second_Global_Variable = "Second Global Variable"; var Local_Variable = "Local Variable"; document.writeln(First_Global_Variable); //prints successfully document.writeln(Second_Global_Variable); //prints successfully document.writeln(Local_Variable); //prints successfully } </script>

Page 26: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

</head><BODY bgcolor=wheat> <pre><Script Language="JavaScript"> Scope_Example(); document.writeln(First_Global_Variable); //prints successfully document.writeln(Second_Global_Variable); //prints successfully document.writeln(Local_Variable); //error message</script></pre></body></HTML>

Page 27: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Table Inside Function

<script language="javascript">document.write("<table border=5>");document.write("<tr><td>name</td>");document.write("<td>ram</td>");document.write("<tr><td>address</td>");document.write("<td>bkt</td>");document.write("<tr><td>phone</td>");document.write("<td>610521</td>");</script>

Page 28: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Defining Functions:

Individual statements used in a computer program are often grouped into logical units called procedures. In JavaScript programming, procedures are called functions.

The lines that compose a function within an HTML document are called the function definition. The syntax for defining a function is:

Function name_of_function (parameters) {

Statements;

}

A function definition consists of three parts:

1. The reserved word function followed by the function name. The reserved word function notified the JavaScript interpreter that the code that follows is a function. As with variables, the name you assign to a function is called an identifier. The same rules and conventions that apply to variable names apply to function names.

2. Any parameters required by the function, contained within parentheses following the function name.

3. The function’s statements, enclosed in curly braces{ }

Page 29: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Function that prints the name of multiple companiesFunction print_company_name(company1, company2, company3)

{

document.writeln(company1);

document.writeln(company2);

document.writeln(company3);

}

Page 30: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

To create a JavaScript Program that Print Company Name:

<HTML>

<HEAD>

<TITLE> Two Functions Program </TITLE>

<Script Language=“JavaScript”

<!- - Hide from incompatible Browsers

Function print_conpany_name(company_name)

{

document.writeln(company_name);

}

//Stop Hiding from Incompatible Browsers - ->

</Script>

Page 31: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Typeof Function<HTML><HEAD> type of function (to define character or number </HEAD><Title>typeof()</title><BODY bgcolor="Red"> <pre><Script Language="JavaScript">var a = 150;var b = "MOTHER";document.writeln("Type of a is" + typeof(a));document.writeln("<br>" + typeof(a));document.writeln("<br>" + "type of b is" + b + typeof(b));</script></pre></body></HTML>

Page 32: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

</HEAD><BODY><Script Language=“Java Script1.3”><!– Hide from incompatible browsers Print_ company_name(“my company”);//Stop Hiding from incompatible browsers - -></script></BODY></HTML>

Page 33: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

To Create a JavaScript Program that contains two functions:

<HTML>

<HEAD>

<TITLE> Two Function Program</TITLE>

<SCRIPT LANGUAGE=“JAVASCRIPT1.2”>

<!- - Hide from incompatible browsersFunction print_message(first_message) { document.writeln(first_message); }Function return_message(second_message) { return “This message was returned from a function”; }//Stop Hiding from Incompatible Browsers - -></Script>

Page 34: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

</HEAD><BODY><PRE><Script Language=“Java Script1.2”><!– Hide from incompatible browsers Print_ message(“’This text was printed from a function”); var return_value = return_message(); document.writeln(return_value);//Stop Hiding from incompatible browsers - -></script></pre></BODY></HTML>

Page 35: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

USING EVENTS

One of the primary ways in which JavaScript makes HTML documents dynamic is through events. You can use JavaScript events to add interactivity between your Web pages and users. An event is specific circumstance that is monitored by JavaScript. The most common events are actions that users take.

EVENT Triggered when

About The loadihng of an image is interrupted

Blur An elemet, such as a radio button, because inactive

Click An element is clicked onces

Change The calue of an element changes

Error There is an erroe when loading a document or image

Page 36: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Focus An element because activeLoad A socument or image loadsMouseOut The mouse moves off an elementMouseOver The mouse moves an elementReset A form resetsSelect A user selects a field in a formSubmit A user submits a formUnload A document unloads

Page 37: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

HTML Tags and EventsElement Description Events

<A> … </A> Link Click

MouseOver

MouseOut

<IMG> Image About

Error

Load

<AREA> area MouseOver

MouseOut

<Body> … </Body> Document body Blur

Error

Page 38: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Focus

Load

Unload

<FRAMESET> … </FRAMESET> Frame set Blur

Error

Focus

Load

Unload<FRAME> … </FRAME> Frame Blur Focus<FORM> … </FORM> Form Submit Reset<INPUT TYPE=“Text”> Text Field Blur Focus

change

Page 39: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

Select

<TEXTAREA> … </TEXTAREA> Text area Blur

focus

change

select

<INPUT TYPE =“SUBMIT”> Submit Click

<INPUT TYPE=“RESET”> Reset Click

<INPUT TYPE=”Radio”> Radio Click

<INPUT TYPE=“CHECKBOX”> Checkbox Click

<SELECT> … </SELECT> Selection Blur

Focus

Change

Page 40: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML><HEAD>

<TITLE>Viewing the array elements of a JavaScript Array</TITLE></HEAD><BODY>

<SCRIPT language = "JavaScript"><!-- Begin Hiding JavaScript friends = new Array(5);friends[0] = "Ananth";friends[1] = "Cedric";friends[2] = "Ketan";document.write(friends[0]+"<BR>");document.write(friends[1]+"<BR>");document.write(friends[2]+"<BR>");

join_crit = friends.join();document.write(join_crit);// End hiding JavaScript --></SCRIPT></BODY></HTML>

Page 41: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML> <HEAD> <TITLE>Creating and Using User Defined Functions</TITLE> <SCRIPT LANGUAGE="JavaScript"> var name = ""; function hello( ) { name = prompt('Enter Your Name:','Name'); alert('Greetings ' + name + ', Welcome to my page!'); }

function goodbye( ) { alert('Goodbye ' + name + ', Sorry to see you go!'); } </SCRIPT> </HEAD> <BODY onLoad="hello( );" onUnload="goodbye( );"> <IMG SRC="Images\Pinkwhit.gif "> </BODY></HTML>

Page 42: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML> <HEAD>

<TITLE>Outputting Text </TITLE> </HEAD> <BODY>

<CENTER><BR><BR><IMG SRC = "Images/sctfamil.gif" Width = 100 Height = 100>

Silicon Chip Technologies.<BR><SCRIPT Language = "Javascript">

document.write("<BR><BR>"); document.write('<IMG SRC = "Images/sctfamil.gif" Width = 100

Height = 100>'); document.write("<B>Silicon Chip Technologies.</B> <BR>")</SCRIPT>

</CENTER> </BODY></HTML>

Page 43: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML> <HEAD><TITLE>EXAMPLE</TITLE> </HEAD>

<BODY><SCRIPT Language = "Javascript"> alert("Welcome To My Web Site!"); document.write('<IMG SRC = "Images/welcome.gif">');</SCRIPT> </BODY></HTML>

Page 44: JAVASCRIPT PROGRAMMING LANGUAGE. Introduction JavaScript is a scripting language. The term scripting language refers to programming languages that are

<HTML> <HEAD><TITLE>Example 2.6 </TITLE> </HEAD> <BODY><SCRIPT LANGUAGE="JavaScript">document. write('<IMG SRC="Images/welcome.gif">');document. write("<H1>Greetings,");document.write(prompt("Enter Your Name:","Name"));document.write(".Welcome to My HomePage!</H1>"); </SCRIPT> </BODY></HTML>