UNIT - 5 Ip
UNIT - 5 Ip
Internet Programming
Unit - V
Client Side Scripting
Client-side scripts can often be looked at if the user chooses to view the source code of the page. JavaScript
code is widely copied and recycled.
What is JavaScript
JavaScript (js) is a light-weight object-oriented programming language which is used by several websites for
scripting the WebPages. It is an interpreted, full-fledged programming language that enables dynamic
interactivity on websites when applied to an HTML document. It was introduced in the year 1995 for adding
programs to the webpages in the Netscape Navigator browser. Since then, it has been adopted by all other
graphical web browsers. With JavaScript, users can build modern web applications to interact directly without
reloading the page every time.
Features of JavaScript
1. All popular web browsers support JavaScript as they provide built-in execution environments.
2. JavaScript follows the syntax and structure of the C programming language. Thus, it is a structured
programming language.
3. JavaScript is a weakly typed language, where certain types are implicitly cast (depending on the
operation).
4. JavaScript is an object-oriented programming language that uses prototypes rather than using classes
for inheritance.
5. It is a light-weighted and interpreted language.
6. It is a case-sensitive language.
7. JavaScript is supportable in several operating systems including, Windows, macOS, etc.
8. It provides good control to the users over the web browsers.
JavaScript Comment
The JavaScript comments are meaningful way to deliver message. It is used to add information about the
code, warnings or suggestions so that end user can easily interpret the code.
The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.
There are two types of comments in JavaScript.
1. Single-line Comment
2. Multi-line Comment
Aman Choudhary Student (BCA – II 2023) Seth G. L. Bihani S. D. PG College
JavaScript Single line Comment
It is represented by double forward slashes (//). It can be used before and after the statement.
Example:
It can be used to add single as well as multi line comments. So, it is more convenient. It is represented by
forward slash with asterisk(/*) then asterisk with forward slash(*/).
Example:
<script>
/* It is multi line comment. It will not be displayed */
document.write("example of javascript multiline comment");
</script>
Variables
Variables are containers for storing data (values). Variables are declared with the var keyword. Storing a value
in a variable is called variable initialization. Variable initialization can be done at the time of variable creation
or at a later point in time when variable needed.
<script type = "text/javascript">
<!--
var name = "Ali";
var money;
money = 2000.50;
//-->
</script>
JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type.
Rules
The general rules for constructing names for variables (unique identifiers) are:
Example
<script>
function abc( ){ var x=10;
//local variable
}
</script>
JavaScript Datatypes
One of the most fundamental characteristics of a programming language is the set of data types it supports.
These are the type of values that can be represented and manipulated in a programming language.
JavaScript provides different data types to hold different types of values. There are two types of data types
in JavaScript.
There are five types of primitive data types in JavaScript. They are as follows:
Conditions Statements
JavaScript supports conditional statements which are used to perform different actions based on different
conditions.
JavaScript If statement
It evaluates the content only if expression is true. The syntax of JavaScript if statement is given below:
if(expression){
//content to be evaluated
}
Example:
<html>
<body>
<script type = "text/javascript">
<!--
var age = 20;
It evaluates the content whether condition is true or false. The syntax of JavaScript if-else statement is given
below:
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false }
Example:
<script> var a=20; if(a%2==0){
document.write("even number");
} else{ document.write("odd
number");
}
</script>
It evaluates the content only if expression is true from several expressions. The syntax of JavaScript if else if
statement is given below:
if(expression1){
//content to be evaluated if expression1 is true
} else
if(expression2){
//content to be evaluated if expression2 is true
} else
if(expression3){
//content to be evaluated if expression3 is true
}
else{
//content to be evaluated if no expression is true
}
Example:
JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple expressions. It is just like else if
statement. The interpreter checks each case against the value of the expression until a match is found. If
nothing matches, a default condition will be used.
Syntax:
switch (expression) {
case condition 1: statement(s)
break;
default: statement(s)
}
Example:
<script> var
grade='B'; var
result;
switch(grade)
{
case 'A':
result="A Grade";
break;
case 'B':
Aman Choudhary Student (BCA – II 2023) Seth G. L. Bihani S. D. PG College
result="B Grade";
break;
case 'C':
result="C
Grade"; break; default:
result="No Grade";
}
document.write(result);
</script>
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while or do while loops. It makes the
code compact. It is mostly used in array.
1. for loop
2. while loop
3. do-while loop
The while Loop
The most basic loop in JavaScript is the while loop. The purpose of a while loop is to execute a statement or
code block repeatedly as long as an expression is true. Once the expression becomes false, the loop terminates.
while (condition)
{
code to be executed
}
Example
<html>
<body>
document.write("Loop stopped!");
//-->
</script>
</body>
</html>
Syntax:
do
{
code to be executed
Example:
<script> var
i=21;
do{
document.write(i + "<br/>"); i++;
}while (i<=25);
</script>
Alert Box
An alert dialog box is mostly used to give a warning message to the users. Alert box gives only one button
"OK" to select and proceed.
Syntax
alert(“Message to display”);
Example
Confirmation Box
A confirmation box is mostly used to take user's consent on any option. It displays a dialog box with two
buttons: OK and Cancel.
If the user clicks on the OK button, the window method confirm( ) will return true. If the user clicks on the
Cancel button, then confirm( ) returns false. Syntax
Example
<script type="text/javascript"> function
msg( ){ var v= confirm("Are u
sure?"); if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
<input type="button" value="delete record" onclick="msg( )"/>
Prompt Box
The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus, it enables
you to interact with the user. The user needs to fill in the field and then click OK.
This dialog box is displayed using a method called prompt( ) which takes two parameters:
(i) a label which you want to display in the text box and (ii) a
default string to display in the text box.
This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the window method
prompt( ) will return the entered value from the text box. If the user clicks the Cancel button, the window
method prompt( ) returns null. Syntax
<variable> = confirm(“Message to display”, “Default Text”);
Example
<html>
<head>
<script type = "text/javascript">
<!--
function getValue() {
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
JavaScript - Events
The change in the state of an object is known as an Event. 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.
Events are a part of the Document Object Model (DOM) Level 3 and every HTML element contains a set of
events which can trigger JavaScript Code.
When JavaScript code is included in HTML, JavaScript react over these events and allow the execution. This
process of reacting over the events is called Event Handling. Thus, JavaScript handles the HTML events via
Event Handlers.
Mouse Events:
Mouseover onmouseover When the cursor of the mouse comes over the
element
Mousedown onmousedown When the mouse button is pressed over the element
Mouseup onmouseup When the mouse button is released over the element
Keyboard events:
Keydown & Keyup onkeydown & When the user press and then release the key
onkeyup
Form events:
Window/Document events
Load onload When the browser finishes the loading of the page
Unload onunload When the visitor leaves the current webpage, the
browser unloads it
Resize onresize When the visitor resizes the window of the browser
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
var arrayname=[value1,value2.....valueN];
Example
Here, you need to create instance of array by passing arguments in constructor so that we don't have
to provide value explicitly.
Example
Array Properties
Here is a list of the properties of the Array object along with their description.
Sr.No. Property & Description
1
index
The property represents the zero-based index of the match in the string
2 length
Array Methods
Here is a list of the methods of the Array object along with their description.
Sr.No. Method & Description
1
concat( )
Returns a new array comprised of this array joined with other array(s) and/or value(s).
2 every( )
Returns true if every element in this array satisfies the provided testing function.
Returns the first (least) index of an element within the array equal to the specified value, or -1 if
none is found.
6 join( )
7 lastIndexOf( )
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if
none is found.
9 pop( )
Removes the last element from an array and returns that element.
10 push( )
Adds one or more elements to the end of an array and returns the new length of the array.
13 reverse( )
Reverses the order of the elements of an array -- the first becomes the last, and the last becomes
the first.
14 shift( )
Removes the first element from an array and returns that element.
18 sort( )
JavaScript Objects
JavaScript is an Object Oriented Programming (OOP) language. Everything is an object in JavaScript. A
javaScript object is an entity having state and behavior (properties and method). For example: car, pen, bike,
chair, glass, keyboard, monitor etc.
object={property1:value1, property2:value2.....propertyN:valueN}
Example
Example