Lesson3 - Advanced JavaScript
Lesson3 - Advanced JavaScript
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
3. ADVANCED JAVASCRIPT
Index:
From:
Mrs. Shilpa Prashant Kate (IT Teacher)
S.B.Patil college of Science & Commerce,Ravet
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
INTRODUCTION
Program is a set of instructions used to produce various kinds of outputs
JavaScript is an interpreted scripting language. An interpreted language is a type of
programming language that executes its instructions directly.
JavaScript was initially created to "make webpages alive". The programs in this language are called
scripts.
3.1.2 DIFFERENCE BETWEEN SERVER SIDE SCRIPTING AND CLIENT SIDE SCRIPTING
Server-side scripting is used at the backend, client- side scripting is used at the frontend
where the source code is not visible or hidden at which users can see source code from the
browser.
the client side (browser). Server-side scripting is
more secure than client-side scripting
client-side scripting does not need any server
When a server-side script is processed it interaction.
communicates to the server.
programming languages such as PHP, ASP.net, Ruby, The client-side scripting language involves
ColdFusion, Python, C# etc. are server side languages such as HTML5, JavaScript etc.
scripting languages.
Server-side scripting is useful in customizing client- side scripts are generally used for
validation purpose and effectively
the web pages and implements the dynamic minimize the load to the server.
changes in the websites.
Special software (web server software) is client side scripts requires web browser as an
required to execute server-side script interface. 3
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
• JavaScript has a built–in multiway decision statement known as Switch. The switch statement test the
value of given expression against a list of case values and when match is found, a block of
statement associated with that case is executed.
• There should not be duplicity between the cases.
• The value for the case must be similar data type as the variable in switch.
• The default statement is not mandatory.
Syntax :
switch(expression)
{
case value1:
statement block 1; break;
case value2:
statement block 2; break;
…………....
case value n:
statement block n; break;
default: 4
statement block ;
}
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
<!DOCTYPE html>
<head><title>Javascript Program
</title></head>
<body>
<h1> use of switch case </h1>
<script type="text/javascript"> var day=6;
switch(day)
{
case 1: alert(“Monday”); break;
case 2: alert(“Tuesday”); break;
case 3: alert(“Wednesday”); break;
case 4: alert(“Thursday”); break;
case 5: alert(“Friday”); break;
case 6: alert(“Saturday”); break;
case 7: alert(“Sunday”); break;
default: alert("Invalid day");
}
</script></body></html> 5
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
document.writeln(i)
6
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
1. for…….loop
This loop executes statements as long as condition becomes true, for-loop is
that it combines initialization, condition and loop iteration (increment or decrement) in
single statement.
Syntax :
for(initialization;condition;iteration)
{
statement block;
}
Note : 'language' attribute of <Script> is replaced by 'type' attribute in all the programs as it is standardized.
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
OUTPUT OUTPUT
8
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
2. W HILE…..LOOP
Syntax:
initialization; while(condition)
{
statement block;
}
9
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
OUTPUT OUTPUT
10
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
<!DOCTYPE html>
<head><title>Table-I</title>
<script type="text/javascript">
function display()
{
var i, a; a=form1.t1.value
for(i=1;i<=10;i++)
{
document.write(a*i + "<br/>");
}
}
</script></head> <body>
Enter number to display table:-
<form name="form1">
<input type="text" name="t1">
<input type="button" value="Display Table" onClick="display()"> 11
</body></html>
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
12
Note : 'language' attribute of <Script> is replaced by 'type' attribute in all the programs as it is standardized.
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
13
Note : 'language' attribute of <Script> is replaced by 'type' attribute in all the programs as it is standardized.
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
Note : 'language' attribute of <Script> is replaced by 'type' attribute in all the programs as it is standardized.
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
16
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
<!Doctype html>
<html>
<head>
<script type="text/javascript">
function getcube(){
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
</head>
<body>
<form>
Enter No:<input type="text" id="number"
name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
</body>
</html>
17
output
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
The innerHTML Property : used to write the dynamic html on the html document.
The innerHTML property is useful for getting html element and changing its content.
The innerHTML property can be used to get or change any HTML element, including
<html> and <body>.
Before button click
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function changeText3() Output
{
var style="<h1 style= 'color:green'>";
var text="Welcome to the HTML5 and Javascript";
var closestyle="</h1>";
document.getElementById('para').innerHTML=style+text+closestyle;
}
</script></head>
<body style="background-color:cyan">
<h1 align="center">
<p id="para">Welcome to the site</p>
<input type="button" onclick="changeText3()" value="click this
button to change above text">
</h1>
</body>
</html> 18
Window Object :
• At the very top of the object hierarchy is the window object. Window object is parent object of all other
objects. It represents an open window in a browser.
• An object of window is created automatically by the browser. Window object represents an open
window in a browser.
Following table shows some of the methods and properties for window object.
19
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
Program :
Before button click
<!DOCTYPE html>
<html> Output
<head> <title> Window Opener and Closer </title>
<script type="text/javascript">
function makeNewWindow()
var newwin=window.open();
newwin.document.write("<p>This is 'New Window'</p>");
newwin.document.body.style.backgroundColor = "skyblue";
</script></head>
<body>
<form>
<input type="button" value="Create New Window"
onClick="makeNewWindow()">
</form> </body>
20
</html>
Program :
Before button click
<!DOCTYPE html>
<html> Output
<head>
<script type="text/javascript">
function sampleFunction()
window.setTimeout(next(), 4000);
}
function next()
</script></head>
<body style="background -color:cyan">
</body> </html>
After button click
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
22
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
23
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
Example :
Example :
var str="Information Technology";
Find the position of the character h in the string txt.
document.write ("length of string is :-" + str.length);
var txt = "abcdefghijklm";
document.write ("Substring is :-" + str.substr (12,10)); var pos = txt. indexOf("h");
Document.write(“Character at 1st location is :”+str.charAt(1)); Output : pos= 8
Output : Example :
Length of string is :-22 var str = "Apple, Banana, Kiwi";
var res = str.substr(7);
Substring is :- Technology
Output: res= Banana, Kiwi
Character at 1st location is : n
<!DOCTYPE html>
<html><head><title>Pincode Validation</title></head>
<body style="color:blue;background-color:cyan">
<form name="form1">
<h1 align="center">
Enter Pincode value:-<input type="text" name="t1"><br><br>
<input type="button" value="Submit value" onClick="validate()">
</h1>
<script type="text/javascript"> function validate()
{
var pincode;
pincode=form1.t1.value;
if(pincode.length==0)
{
alert("please check, enter value"); form1.t1.focus();
}
else if(isNaN(pincode))
{
alert("please, enter integer number only"); form1.t1.focus();
}
else if(pincode.length<6||pincode.length>8)
{
alert("pincode length range between 6 to 8"); form1.t1.focus(); }
else
alert("Pincode is accepted"); 25
}
</script> </body></html>
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
Summary
26
PIMPRI CHINCHWAD EDUCATION TRUST’S
S.B.PATIL COLLEGE OF
Science and Commerce, Ravet
Thank you !
27