Documente Academic
Documente Profesional
Documente Cultură
Copyright 2007 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture.
Contents
What is JavaScript Features of JavaScript JavaScript in HTML page
Objectives
At the end of this section, you should be able to: Understand what JavaScript is Understand the need of JavaScript Understand how to use JavaScript
What is JavaScript?
Javascript is a scripting language which can be used in Web pages to do client side validations. It is supported by all the browsers. It is also used to validate web pages to improve design and add functionality to web pages.
Features of JavaScript
Features of Java
Programming Tool for interactive HTML pages Interpreted Language Saves time as validation is done on Client side Event-Driven Programming Detects Client browser, hence platform independent
Displayed in browser
Copyright 2007 Accenture All Rights Reserved.
<html> <head> <script language=javascript"> some statements </script> </head> </html> Advantage The script will be loaded before anyone uses it.
Copyright 2007 Accenture All Rights Reserved.
<html> <head> </head> <body> <script type=javascript"> some statements </script> </body> </html> Advantage The script generates the content of the page.
Copyright 2007 Accenture All Rights Reserved.
Example
<html> <head> <script type="text/javascript"> document.write(Welcome to Javascript - Head Section") </script> </head> <body> <script type="text/javascript"> document.write(Welcome to Javascript - Body Section") </script> </body> </html>
Output:
What is a Variable?
Information is stored in variables. Its value can be change during the execution of script. It can be referenced by its name to see its value or to change its value. Datatype supported Variant. Variant can store any type of data.
Copyright 2007 Accenture All Rights Reserved.
Variant
Integer Boolean Float Char
Variant
Declaring Variables
There are two ways of declaring a variable. Implicit Declaration Explicit Declaration Implicit Declaration Declare variables by using its name directly in the script. For example : name=Tom Explicit declaration - Using var keyword For example : var name name=Tom
Value of variable
Operators
The operators in JavaScript are the same as in any other language or scripting engine. Each operator denotes a specific operation. They can be classified into three categories: Arithmetic Operators + , - ,* , / ,% Logical Operators && , || , ! Relational Operators < , > , = , >= , <= , == ,!=
Conditional Statements
In JavaScript we have three conditional statements: if statement if...else statement if... else if statement switch statement
else if (age >5 && age<=15) { else { document.write( Not ready to disclose age); } document.write(You are a teenager); }
Output:
For example
<html> <head> <script type="text/javascript"> var name=prompt(" Enter your name"); document.write(name + ",Come let's rock !"); </script> </head></html>
Copyright 2007 Accenture All Rights Reserved.
JavaScript - Functions
Can called from anywhere within the page Can be placed both in the body and in the head section Can be independent or can also return a value Begins with the keyword function followed by the name of function along with ( ) Statements within the function are enclosed in { }
Output:
Events in JavaScript
Interaction between the user and the web page is through a set of events. Events are pieces of code that are run when an action is taken by the user. Events in windows generate messages. These messages update the operating system and the application about the activities going on.
Events (contd)
Each web browser has it's own specific set of events that can be triggered. Following are some of the events which are supported by both Internet Explorer and Netscape Navigator (Ver.4.0 and above)
OnClick OnBlur OnFocus
Events (contd)
OnClick This is the event that is implemented most often and supported by all browsers. It is added to <input type="button"> tags and <a> links. This event is triggered when the button or the hyperlink is clicked For example: <input type="Button" value="Save" onclick=alert('This is the event of Button')">
When the Button is clicked, the alert box will be displayed.
Events (contd)
OnBlur Supported by almost all browsers This event occurs when an element loses focus , that is when the user selects another element. It can be added to any element. For example:
<html> <form> <input type="Button" value="Save" onblur="confirm('This is the Blur event on SAVE button')"> <input type="Button" value="Cancel"> </form> </html>
When the user moves the focus from SAVE button to CANCEL button , the confirm message will be displayed.
Events (contd.)
OnFocus
This event occurs when the cursor focus is on the element and can be applied to all elements. It can be added to Select ,text boxes and textareas. It is activated only after the element loses focus For example:
<html> <head> <script type="text/javascript"> function disp() { alert("The focus is on the textbox"); </script> </head> <form> <input type="text" onfocus="disp()"><br> <input type="Button" value="Save"> </form> </html>
When the user places the focus on the Textbox , the disp() function will be invoked.
Copyright 2007 Accenture All Rights Reserved.
Key Points
JavaScript is a scripting language for html pages on the WWW and Intranets. Only one datatype Variant JavaScript supports conditional and looping statements. Functions form an integral part of JavaScript OnClick ,OnBlur and OnFocus are some of the events supported by JavaScript.
Copyright 2007 Accenture All Rights Reserved.
Available Resources
Sites
http://www.w3schools.com/JS/default.asp http://www.tutorialized.com/tutorials/Javascript http://www.beansoftware.com/ASP.NET-Tutorials/JavascriptFunctions.aspx http://www.java-scripts.net/
On-Line Books
www.programmerworld.net/personal/free_books.htm www.learnasp.com/freebook/learn/ www.asp.net/community/books/