Unit - 5 - Client Side Scripting
Unit - 5 - Client Side Scripting
Client Side
Scripting
TOPICS
Introduction to JavaScript
Declaring variables
Scope of variables
Functions
Event handlers (onclick, onsubmit etc)
Document Object Model
Form validation
Simple AJAX application.
Hibernate framework.
What is JavaScript ?
If you need a script to run as the page loads so that the script generates
content in the page, the script goes in the <body> portion of the
document.
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("Hello World")
</script>
<p>This is web page body </p>
</body>
</html>
JavaScript in <body> and <head> sections:
<html>
<head>
<script type="text/javascript">
function sayHello() { alert("Hello World") }
</script>
</head>
<body>
<script type="text/javascript">
document.write("Hello World")
</script> <input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>
JavaScript in External File :
<html>
<head>
<script type="text/javascript" src="filename.js" >
</script>
</head>
<body> ....... </body>
</html>
function sayHello()
{
alert("Hello World")
}
JavaScript Reserved Words
== Checks if the value of two operands are equal or not, if yes then (A == B) is not true.
condition becomes true.
!= Checks if the value of two operands are equal or not, if values are not (A != B) is true.
equal then condition becomes true.
> Checks if the value of left operand is greater than the value of right (A > B) is not true.
operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right (A < B) is true.
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value (A >= B) is not true.
of right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of (A <= B) is true.
right operand, if yes then condition becomes true.
The Logical Operators:
= Simple assignment operator, Assigns values from right side operands to left side C = A + B will assigne value of A + B into C
operand
+= Add AND assignment operator, It adds right operand to the left operand and C += A is equivalent to C = C + A
assign the result to left operand
-= Subtract AND assignment operator, It subtracts right operand from the left C -= A is equivalent to C = C - A
operand and assign the result to left operand
*= Multiply AND assignment operator, It multiplies right operand with the left C *= A is equivalent to C = C * A
operand and assign the result to left operand
/= Divide AND assignment operator, It divides left operand with the right operand C /= A is equivalent to C = C / A
and assign the result to left operand
%= Modulus AND assignment operator, It takes modulus using two operands and C %= A is equivalent to C = C % A
assign the result to left operand
The Conditional Operator (? :)
Op Description Example
era
tor
? : Conditional If Condition is true
Expression ? Then value X :
Otherwise value Y
JavaScript if...else Statements
JavaScript supports following forms
of if..else statement:
if statement
if...else statement
if...else if... statement.
if statement
The if statement is the fundamental control
statement that allows JavaScript to make decisions
and execute statements conditionally.
<script type="text/javascript">
var age = 20; if( age > 18 )
{
document.write("<b>Qualifies for driving</b>");
}
</script>
if...else statement
The if...else statement is the next form of control statement
that allows JavaScript to execute statements in more
controlled way.
<script type="text/javascript">
var age = 15;
if( age > 18 )
{
document.write("<b>Qualifies for driving</b>"); }
Else
{
document.write("<b>Does not qualify for driving</b>");
}
</script>
JavaScript Switch Case
<script type="text/javascript">
var grade='A'; document.write("Entering switch block");
switch (grade)
{
case 'A': document.write("Good job<br />"); break;
case 'B': document.write("Pretty good<br />"); break;
case 'C': document.write("Passed<br />"); break;
case 'D': document.write("Not so good<br />"); break;
case 'F': document.write("Failed<br />"); break;
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block"); </script>
JavaScript while Loops
While writing a program, there may be a situation when
you need to perform some action over and over again.
The while Loop:
<script type="text/javascript">
var count = 0;
document.write("Starting Loop" + "<br />");
while (count < 10)
{
document.write("Current Count : " + count + "<br />");
count++;
}
document.write("Loop stopped!");
</script>
The do...while Loop:
<script type="text/javascript">
var count = 0;
document.write("Starting Loop" + "<br />");
do{ document.write("Current Count : " + count + "<br
/>");
count++;
}while (count < 0);
document.write("Loop stopped!");
</script>
JavaScript for Loops
<script type="text/javascript">
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++)
{
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");
</script>
JavaScript Loop Control
<script type="text/javascript">
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 10)
{ x = x + 1;
if (x == 5)
{ continue; // skill rest of the loop body }
document.write( x + "<br />"); }
document.write("Exiting the loop!<br /> ");
</script>
JavaScript Functions
A function is a group of reusable code which can be
called anywhere in your program.
JavaScript allows us to write our own functions as
well.
Function Definition:
<script type="text/javascript">
function functionname(parameter-list)
{ statements }
</script>
Example
<script type="text/javascript">
function sayHello() //function definition
{
alert("Hello there");
}
</script>
<script type="text/javascript">
sayHello(); //calling function
</script>
Function Parameters:
A function can take multiple parameters separated by
comma.
<script type="text/javascript">
function sayHello(name, age)
{
alert( name + " is " + age + " years old.");
}
</script>
<script type="text/javascript">
sayHello(‘Mili', 1 );
</script>
The return Statement
<html>
<head>
<script type = "text/javascript">
function concatenate(first, last)
{
var full;
full = first + last;
return full;
}
function secondFunction()
{
var result;
result = concatenate(‘Mili', ‘Kiran');
document.write (result );
}
</script>
</head>
<body>
<form> <input type = "button" onclick =
"secondFunction()" value = "Call Function">
</form>
</body>
</html>
Event Handlers
JavaScript's interaction with HTML is handled through
events that occur when the user or the browser
manipulates a page.
When the page loads, it is called an event.
When the user clicks a button, that click too is an event.
Other examples include events like pressing any key,
closing a window, resizing a window, etc.
Ex: Onclick(),
Onsubmit()
Onmouseover()
Onmouseoue()
onclick Event Type
<html>
<head>
<script type = "text/javascript">
<!--function sayHello()
{
alert("Hello World")
} //-->
</script>
</head>
<body>
<form> <input type = "button" onclick = "sayHello()" value =
"Say Hello" />
</form>
</body>
</html>
onsubmit Event Type
<html>
<head>
<script type = "text/javascript">
<!-- function validation()
{
all validation goes here ......... return either true or false
} //-->
</script>
</head>
<body>
<form method = "POST" action = "t.cgi" onsubmit = "return
validate()">
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
Document Object Model
With the object model, JavaScript gets all the power it needs to
create dynamic HTML:
JavaScript can change all the HTML elements in the page
JavaScript can change all the HTML attributes in the page
JavaScript can change all the CSS styles in the page
JavaScript can remove existing HTML elements and attributes
JavaScript can add new HTML elements and attributes
JavaScript can react to all existing HTML events in the page
JavaScript can create new HTML events in the page
HTML DOM methods are actions you can perform (on HTML
Elements).
HTML DOM properties are values (of HTML Elements) that you
can set or change.
The DOM Programming Interface
The HTML DOM can be accessed with JavaScript (and
with other programming languages).
In the DOM, all HTML elements are defined
as objects.
The programming interface is the properties and
methods of each object.
A property is a value that you can get or set (like
changing the content of an HTML element).
A method is an action you can do (like add or deleting
an HTML element).
Example
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "H
ello World!";
</script>
</body>
</html>
Method Description
Property Description
element.innerHTML = new html Change the inner HTML of an element
content
Method Description
Method Description
<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change
Content</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>
</body>
</html>
Hibernate framework