Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Icons Used
Hands on Exercise
Questions
Tools
Coding Standards
Case Study
Demonstration
!or"s#o$
Objectives
After completing this chapter you will be able to understand,
What is java script? How to develop Client Side programming using java script? Basic building bloc s of !ava script"
A client side script is a small program which is executed by the browsers. Examples : Javascript , VBscript Need of client side script? For validating the user data before it reaches the server. Example: Login name and password fields are mandatory in a login page. Validation can be done by java script. Perform some dynamic functionality based on user navigations. Example: ser hovers above a image! dynamically changes the image. "isable the salary text field if user selects unemployed radio button. &
User '(ndia)
erver 'U*)
erver validates the login page
No &lient side scripting : #f each transaction &arrow depicted' ta%es ( seconds the total transaction ta%es ) seconds. &lient side script implemented: server calls will not happen if the user data is wrong. Assuming script validation ta%es * second for the validating the login name the transaction could be completed in * second &reducing the response time by )+,'.
'
What is Java
cript?
JavaScript is a client side scripting language which can be used to perform client side operations. -ava scripts are executed by the browser engine. Example: Validations in fields $how tool tips Perform some operations on mouse clic%. .hange a font color on mouse over.
Advantages of Java
cript
-ava $cript is used for performing the basic sanity chec%s on the user re/uest before it is submitted to the server! thus improving the transactions response time. 0his also reduces the load on the server as some logic is executed by the client browser. 0his also reduces the networ% traffic by ensuring that no invalid user re/uests are transmitted over the networ%.
Uses Of Java
.lient side validation
cript?
"ynamic application of styles Examples: .hange the font color of a text dynamically. -ava$cript can react to events li%e onclick , onblur, onfocus etc Examples: Perform a validation on .lic%. 1n focus of a text box clear the values.
+,
++
+2
+3
+%
+&
#iterals
Literals are fixed values that appear in a -ava $cript program. 3e! Java cript literals, Integer Literals: #ntegers can be expressed in decimal! hexadecimal! and octal. var value4(H< Object Literals : An object literal is a list of ?ero or more pairs of property names and associated values of an object! enclosed in curly braces &:='. var animal4:cat!dog= String Literals : A string literal is ?ero or more characters enclosed in double or single /uotation mar%s. var number4C(HD Float Literals : 9epresents floating point values. var pi4(.*@ +'
$onditional
tate)ents* If++else
6efinition: sed to execute some code if the condition is true and another code if the condition is false. $ntax
if 'condition)4 22code to be executed if condition is true 5 else 4 22code to be executed if condition is not true 5 if 'a1b)4 document.!rite'"a is greater than b%)7 5 else 4 document.!rite'"b is greater than a%)7 5
Example
&'
$onditional
$ntax
if 'condition)4 22code to be executed if condition is true 5 else if 'condition2) { 22code to be executed 5 else 4 22code to be executed 5
Example
var time8,97 if 'time0,,)4 document.!rite'":ood #orning%)7 5 else if 'time< !) { document.!rite'":ood Noon%)7 5 else 4 document.!rite'":ood Evening%)7 5
&(
sed for iterating through a bloc% for the specified number of times.
6o..?hile : 0he do...while loop is a variant of the while loop. 0his loop will execute the bloc% of code atleast 1B.8! and then it will repeat the loop as long as the specified condition is true.
do 4 ""code to be e#ecuted in loop 5 !hile 'var08endvalue)7 var i8,7 do 4 document.!rite'i)7 5 !hile 'i08,>)7
,-pressions
-ava$cript e#pressions are similar to expressions in any other programming language. <hree t$pes of expressions: ,. -. .. A. /rithmeticI evaluates to a number. $#ample : var sum 4 a ; b< tring: evaluates to a character string. $#ample : var name 45FredC< @ogical: evaluates to true or false. $#ample : var flag 4 a>b; &onditional Expression: A conditional expression can have one of the two values based on a condition. $ntax: var value4&condition' J val* I valH #f condition is true! the expression has the value of val*! 1therwise it has the value of valH. Example: status 4 &age 34 *K' J 5adult5 I 5minorD Assigns the value 5adult5 to the variable status if age is eighteen or greater. 1therwise! it assigns the value 5minor5 to status.
22
Operators
-ava$cript operators are similar to the operators in any other programming language. -ava$cript supports the following types of operators *. Arithmetic A $#ample: ;! L H. Mitwise A $#ample: N! O (. .omparison A $#ample: 3! 2 and P4 @. Assignment F $#ample: 4 Q. Logical F $#ample: OO! RR
23
Ti)e To .eflect
Associates to /uic%ly summari?e the following before ending the session 7hat ma%es java script useful for form validationJ Sow to declare a variable in java scriptJ 7hat is the tag used to declare java script codeJ 7hat is the construct used for executing a statement repeatedlyJ
2%
cript !e)o
+bBective: 0his is a demo to show how to write a java script method inside a S0TL page. Croblem: Print the first HQ odd numbers using -ava$cript. &omponents to be developed I myPage.html
2&
2'
2(
Advance Java