Unit-II Web Technologies
Unit-II Web Technologies
in
Unit-II
JavaScript
2.1 Introduction :
JavaScript is a very powerful client-side scripting language. JavaScript is used mainly for
enhancing the interaction of a user with the webpage. JavaScript is also being used widely in game
development and Mobile application development.
JavaScript was developed by Brendan Eich in 1995. The language was initially called LiveScript and
was later renamed JavaScript.
JavaScript is a Scripting language . It is a light-weighted and interpreted language and it is a case-
sensitive language.
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box
and prompt dialog box) and Displaying clocks etc.
How to Add a Script to Your Pages :
JavaScript can either be embedded in a page or placed in an external script file (rather like CSS).
You add scripts to your page inside the <script> element. The type attribute on the opening
<script> tag indicates what scripting language will be found inside the element, so for JavaScript
you use the value text/JavaScript .
Syntax:-
<script type=”text/javascript”>
document.write(“content”);
</script>
In the above syntax ,
The script tag specifies that we are using JavaScript.
The text/javascript is the content type that provides information to the browser about the data.
The document.write() function is used to display dynamic content through JavaScript.
The write() method to add a new line of text into the web page (and the web page is represented
using the document object).
The < noscript > Element
The <noscript> tag in HTML is used to display the text for those browsers which does not support
script tag or the browsers disable the script by the user. This tag is used in both <head> and
<body> tag.
This tag is used in those browsers only which does not support scripts.
Syn : <noscript> content</noscript>
1
Simple Program : Displaying a Line of Text in a web Page (Script inside the body tag)
<html>
<head>
<title> JavaScript Example </title>
</head>
<body>
<script type="text/javascript">
document.write("Welcome to JavaScript Programming")
</script>
</html>
2. You can keep the JavaScript code in a separate external file and then point to that file from
your HTML document.
• An external JavaScript file must be saved by .js extension. It is recommended to embed all
JavaScript files into a single file. It increases the speed of the webpage.
External.js
document.write("Welcome to JavaScript Programming")
External.html
<html>
<head>
<title> JavaScript Example </title>
</head>
<body>
<script src=”external.js”>
</script>
</html>
JavaScript uses reserved keyword var to declare a variable. A variable must have a unique name.
You can assign a value to a variable using equal to (=) operator when you declare it or before
using it.
If you declare a variable outside a function, all the functions on your page can access it . The
lifetime of these variables starts when they are declared and ends when the page is closed.
Variables outside a functions are called global variables.
JavaScript provides three important Dialog Boxes, which include Alert Dialog Box for users,
Confirmation Dialog Box, and Prompt Dialog Box.
• Alert Dialog box
• Confirmation Dialog box
• Prompt Dialog box
<html>
<head>
<title>Alert popup</title>
<script>
window.alert("Welcome to JavaScript Programming");
</script>
</head>
</html>
<html>
<head>
<title>prompt popup</title>
<script>
var res=window.prompt("enter user name");
window.alert("User name is "+res);
</script>
</head>
</html>
To display a Confirmation Dialog Box you call the confirm(message) function, in which
the message is one requesting an user to confirm.
If the user clicks the OK button, this function returns true, otherwise if the user clicks the No button,
this function returns false.
2.4 Operators
An operator performs some operation on single or multiple operands (data value) and produces a
result.
For example 1 + 2, where + sign is an operator and 1 is left operand and 2 is right operand.
+ operator adds two numeric values and produces a result which is 3 in this case.
JavaScript includes following categories of operators.
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. String Operators
6. Conditional Operator
2.4.1 Arithmetic Operators
Arithmetic operators perform arithmetic operations upon operands
Symbol Description Example (x=10 and y=3) Result
+ Addition x+y 15
- Subtraction x-y 8
* Multiplication x*y 30
/ Division x/y 5
% Modulus (division remainder) x%y 1
<html>
<head>
<title>Comparison Operator</title>
</head>
<body>
<script>
var x=12,y=3
document.write("x==y : "+(x==y))
document.write("<br>x!=y : "+(x!=y))
document.write("<br>x>y : "+ (x>y))
document.write("<br>x>=y : "+(x>=y))
document.write("<br>x<=y : "+(x<=y))
document.write("<br>x less than y : "+ (x<y))
</script>
</body>
</html>
10
if (condition)
{
code to be executed if condition is true
}
if statements allow code to be executed when the condition is true, else statements allow to
executed when condition is false.
Syntax:
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}
12
if(condition_1)
{
if(condition_2)
{
inner if-block of statements;
...
}
...
}
13
<html>
<head>
<title>Nested if </title>
</head>
<body>
<script>
var x=10,y=20
if(x>5)
if(y>5)
document.write("x and y are > 5 ")
else
document.write("x is <= 5")
</script>
</body>
</html>
if(condition_1) {
condition_1 true-block;
...
}
else if(condition_2){
condition_2 true-block;
condition_1 false-block too;
...
}
<html>
<head>
<title>if else if </title>
</head>
<body>
14
<script>
var a=15
if(a==10)
document.write("a is equal to 10")
else if (a==15)
document.write("a is equal to 15")
else if (a==25)
document.write("a is equal to 25")
else
document.write("a is not equal to 10, 15 or 25")
</script>
</body>
</html>
The switch is a conditional statement like if statement. Switch is useful when you want to execute
one of the multiple code blocks based on the return value of a specified expression.
Syntax:
switch (expression)
{
case option1:
code to be executed if expression is what is written in option1
break;
case option2:
code to be executed if expression is what is written in option2
break;
default:
code to be executed if expression is different from option1,and option2,
}
var a=2;
switch(a)
{
case 1: document.write("Case 1 Executed");
break;
case 2: document.write("Case 2 Executed");
break;
case 3: document.write("Case 3 Executed");
break;
default:document.write("default case Executed");
}
</script>
</body>
</html>
2.6 Looping
Looping statements are used to execute the same block of code a specified number of times.
2.6.1 While
In a while loop, a code block is executed if a condition is true and for as long as that condition
remains true.
Syntax:
while (condition)
{
code to be executed
}
Example 1 Program on while loop:
<html>
<head>
<title>while Loop</title>
</head>
<body>
<script type="text/JavaScript">
var i=1;
while(i<11)
{
document.write(i+"*3="+(i*3)+"<br>");
i++;
}
</script>
</body>
</html>
16
<html>
<head>
<title>while Loop</title>
</head>
<body>
<script>
let i=1;
while(i<10)
{
document.write("<br>i="+i);
i=i+1;
}
</script>
</body>
</html>
2.6.2 do . . . while
The do-while loop is similar to while loop the only difference is it evaluates condition expression
after the execution of code block. So do-while loop will execute the code block at least once.
Syntax:
do
{
code to be executed
} while (condition)
Example 1 Program on do-while loop:
<html>
<head>
<title>do-while Loop</title>
</head>
<body>
<script type="text/JavaScript">
var i=1;
do
{
document.write(i+"*5="+(i*5)+"<br>");
i++;
} while(i<11)
17
</script>
</body>
</html>
<html>
<head>
<title>do-while Loop</title>
</head>
<body>
<script>
let i=1;
do
{
document.write("<br>i="+i);
i=i+1;
}while(i<10)
</script>
</body>
</html>
2.6.7 For
for loop to execute code repeatedly.
Syntax:
<html>
<head>
<title>for Loop</title>
</head>
<body>
<script type="text/JavaScript">
var i;
for(i=1;i<11;i++)
{
document.write(i+"*2="+(i*2)+"<br>");
18
}
</script>
</body>
</html>
<html>
<head>
<title>For Loop</title>
</head>
<body>
<script>
for(let i=1;i<10;i++)
{
document.write("<br>i="+i);
}
</script>
</body>
</html>
2.7 Break and continue
• The break statement in javaScript is used to terminate a conditional or looping statement.
• The continue statement is used to move the execution control to the beginning of the looping
statement. When the continue statement is encountered in a looping statement, the execution
control skips the rest of the statements in the looping block and directly jumps to the beginning
of the loop.
Example program on break and continue
<html>
<head>
<title>break and continue</title>
</head>
<body>
<script type="text/JavaScript">
var i;
for(i=1;i<11;i++)
{
if (i==6)
break;
else if (i==3)
continue;
19
else
document.write(i+"<br>");
}
</script>
</body>
</html>
Syntax
labelname:
statements
Example Program on Labeled break:
<html>
<head>
<title>Using break statement with Label</title>
</head>
<body>
<script>
stop:
for(var i=0;i<10;i++)
{
for(var j=0;j<5;j++)
{
if(i==3)
break stop;
document.write("*");
}
document.write("<br>");
}
</script>
</body>
</html>
20
<html>
<head>
<title>Using continue statement with Label</title>
</head>
<body>
<script>
nextrow:
for(var i=0;i<=5;i++)
{
document.write("<br>");
for(var j=1;j<=5;j++)
{
if(j>i)
continue nextrow;
document.write("*");
}
}
</script>
</body>
</html>
21
<html>
<head>
<title>Square Function</title>
</head>
<body>
<script>
document.write("<h2> Square the numbers from 1 to 10 </h2>");
22
function square(y)
{
return y*y
}
for(var x=1;x<=10;x++)
document.write("<br> The square of " + x +" is "+square(x));
</script>
</body>
</html>
<html>
<head>
<title>Maximum of Three Numbers</title>
</head>
<body>
<script>
var a=window.prompt("Enter First number");
var b=window.prompt("Enter Second number");
var c=window.prompt("Enter Third number");
document.writeln("First number: "+a);
document.writeln("<br>Second number: "+b);
document.writeln("<br>Third number: "+c);
function maximum(x,y,z)
{
return Math.max(x,Math.max(y,z));
}
23
var maxvalue=maximum(a,b,c);
document.writeln("<br> Maximum is: "+maxvalue);
</script>
</body>
</html>
1. Global scope
2. Local scope
Global Scope
Variables declared outside of any function become global variables. Global variables
can be accessed and modified from any function.
Local Scope
Variables declared inside any function with var or let keyword are called local variables.
Local variables cannot be accessed or modified outside the function declaration.
24
<html>
<head>
<title>Scope & Global Variables </title>
<script>
var x=1;
function start()
{
var y=5;
document.write("Local Variable y value :"+y);
A();
}
function A()
{
document.write("<br>Global Variable x value :"+x);
}
</script>
</head>
<body onload="start()">
</body>
</html>
25
Javascript provides seven global functions. Object is one such global constructor
function in JavaScript which is used to new create objects.
escape() :-
The escape() function takes a string as a parameter and encodes it so that it can be
transmitted to any computer in any network which supports ASCII characters.
Syntax:
escape(string)
eval() :-
The eval() function in JavaScript is used to evaluate the expression, which evaluates the
specified string as JavaScript code and executes it.
Syntax:
eval(string)
isFinite() :-
The JavaScript number isFinite() method determines whether the given value is a finite
number. It returns true if the value is a finite number, otherwise it returns false.
Syntax:
Number.isFinite(num)
isNaN() :-
isNaN() method returns true if a value is Not-a-Number. Number.isNaN() returns true if
a number is Not-a-Number
Syntax:
isNaN(value)
parseFloat () :-
The JavaScript number parseFloat() method parses a string argument and converts it
into a floating point number. It returns NaN, if the first character of given value cannot be converted
to a number.
Syntax:
Number.parseFloat(string)
parseInt () :-
The JavaScript number parseInt() method parses a string argument and converts it into
an integer value. With string argument, we can also provide radix argument to specify the type of
numeral system to be used.
26
radix - It is optional. An integer between 2 and 36 that represents the numeral system to be used.
unescape() :-
The unescape() function takes a string as a parameter and returns a string in which all
characters previously encoded with escape are decoded
Syntax:
unescape(string)
<html>
<head>
<title>Global Functions</title>
<script>
document.write(escape("Javascript global functions"));
document.write(unescape("<br>Javascript global functions"));
var a=10, b=20, c=30;
document.write("<br>"+eval("a+b+c"));
document.write("<br>"+parseFloat("10"));
document.write("<br>"+parseInt("10.56"));
document.write("<br>"+isFinite(123));
document.write("<br>"+isNaN(“Hello”));
</script>
</head>
</html>
27
3.1 Recursion :
Recursion is a process of calling itself. A function that calls itself is called a recursive function.
Here, the recurse() function is a recursive function. It is calling itself inside the function.
• A recursive function must have a condition to stop calling itself. Otherwise, the function is called
indefinitely.
• Once the condition is met, the function stops calling itself. This is called a base condition.
• To prevent infinite recursion, you can use if...else statement (or similar approach) where one
branch makes the recursive call, and the other doesn't.
So, it generally looks like this.
function recurse()
{
if(condition)
{
recurse();
}
else
{
// stop calling recurse()
}
}
recurse();
28
<html>
<head>
<title>Recursive Factorial Function</title>
</head>
<body>
<script>
document.write("<h3> Factorial of 1 to 10 </h3>");
function factorial(n)
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}
for(var i=0;i<=10;i++)
document.write("<br>"+ i +"!    "+factorial(i));
</script>
</body>
</html>
29
Recursion Iteration
In iteration, there is a repeated
execution of the set of instructions.
Recursion is the process of calling a In Iteration, loops are used to
function itself within its own code. execute the set of instructions
repetitively until the condition is
false.
The format of iteration includes
There is a termination condition is initialization, condition, and
specified. increment/decrement of a
variable.
The termination condition is Here, the termination condition is
defined within the recursive defined in the definition of the
function. loop.
Unit-II Questions
1. Explain about JavaScript
2. Write a short note on Dialog box / Pop-up boxes (alert, prompt & confirm
3. Explain about Operators in JavaScript
4. Explain about Conditional statements with an example programs
5. Explain about Looping statements with an example programs
6. Explain about break, continue, Labeled break and continue statements
7. Explain in detail about functions
8. Explain about variables & scope rules in JavaScript
9. Global functions in JavaScript
10. Recursion
11. Recursion vs iteration
30