SlideShare a Scribd company logo
Java Training Center
(No 1 in Training & Placement)
1
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Java Training Center
(No.1 in Training & Placement)
Javascript
Author
SomPrakashRai
Java Training Center
(No 1 in Training & Placement)
2
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Welcome Example(Using Internal Script)
<html>
<head>
<title>1.Welcome to Javascript</title>
<script type="text/javascript">
document.write('welcome to javascript');
</script>
</head><body>
</body></html>
Java Script Using External Script.
<html>
<head>
<title>2.External Javascript</title>
<script type="text/javascript" src="js/script.js">
</script>
</head><body>
</body></html>
script.js
document.write("Welcome to JavaScript from external by Java Training Center");
3.No-Script
<html>
<head>
<title>3.No Script By Java Traninig Center</title>
<script type="text/javascript" src="script.js">
</script>
</head><body>
<noscript><h2>Sorry, this page requires JavaScript.</h2>
</noscript></body>
</html>
Script.js
document.write("Welcome to JavaScript from external by Java Training Center");
4.Variable
<html>
<head>
<title>2.External Javascript</title>
<script type="text/javascript" src="script.js">
</script></head>
<body></body>
</html>
Java Training Center
(No 1 in Training & Placement)
3
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Script.js
var x=10;
var y=20;
var z=x+y;
var name="JTCNOIDA";
/*we can store the value*/
document.write("the value of x is "+x);
document.write("<br>");
document.write("the value of y is "+y);
document.write("<br>");
document.write("the value of z is "+z);
document.write("<br>");
document.write(name+" is a professional training institute for Java ");
5.arithmetic_operators
<html>
<head>
<title>2.External Javascript by Java Training Center</title>
<script type="text/javascript" src="script.js">
</script>
</head>
<body>
<!-- -->
</body>
</html>
Script.js
var x=10;
var y=20;
var z=x/y;
var name="JTCNOIDA";
document.write("The value of x is "+x);
document.write("<br>");
document.write("The value of y is "+y);
document.write("<br>");
document.write("The value of z is "+z);
document.write("<br>");
document.write(name+" is a Professional training institute");
6. arithmetic_operators
<html>
<head>
<title>5.Assignment Operators BY JAVA TRAINING CENTER</title>
<script type="text/javascript" src="script.js">
</script>
</head><body>
Java Training Center
(No 1 in Training & Placement)
4
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
</body></html>
Scprit.js
var x=10;
var y=20;
document.write("The value of x is "+x);
document.write("<br>");
document.write("Operator Test by JTC");
document.write("<br>");
var z=x+=y;
//z=x=x+y
//var z=x/=y;
//var z=x%=y;
//x+=y ; x=x+y;
document.write("The value of x is "+x);
document.write("<br>");
document.write("The value of y is "+y);
document.write("<br>");
document.write("The value of z is "+z);
7.+operator_on_string
<html>
<head>
<title>7.+ Operator on String by Java Training Center</title>
<script type="text/javascript" src="script.js">
</script></head><body>
</body></html>
Script.js
var x=10;
var y=20;
var a="10";
var b="20";
document.write("The value for adding two strings a + b = "+a+b);
document.write("<br>");
document.write("The value for adding number and string x + b = "+x+b);
8. Alert
<html>
<head>
<title>8.Alert boxes by Example By JAVA TRAINING CENTER</title>
<script type="text/javascript" src="script.js">
Java Training Center
(No 1 in Training & Placement)
5
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
</script></head>
<body></body>
</html>
Script.js
alert("Click OK to Proceed");
9.Confirm
<html>
<head>
<title>8.Confirm box by Java Training Center</title>
<script type="text/javascript" src="script.js">
</script></head>
<body></body>
</html>
Script.js
confirm("Click OK or Cancel");
10.Prompt
As Above (Html)
Script.jsp
var x=prompt("Enter the number");
alert("The Value that u entered is "+x);
/*prompt considers string value*/
11.if –Statement
Script.js
var x = prompt("Enter number 10");
/*string*/
alert(typeof(x));
if(x==="10")
{
alert("Its Correct");
}
else {
alert("Its False");
}
12.if-else –Statement.
Script.js
var num1 = prompt("Enter num1","10");
var num2 = prompt("Enter num2","20");
var num3 = parseInt(num1);
Java Training Center
(No 1 in Training & Placement)
6
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
var num4 = parseInt(num2);
/*parseInt - converting string to integer*/
if(num3 > num4 )
{
alert( num3 +" is Bigger");
}
else
{
alert( num4 +" is Bigger");
}
13.if –else –if Statement.
Script.js
var corejava = parseInt(prompt("Enter ur marks in Core Java"));
var advancejava = parseInt(prompt("Enter ur marks in Advance Java "));
var struts = parseInt(prompt("Enter ur marks in Struts"));
var total=corejava+advancejava+struts;
var average = total/3;
alert("average is "+average);
//logical operators
//&& (AND ) - all the conditions should satisfy
//|| (OR) - atleast one conditions should satisfy
if(corejava>=35&&advancejava>=35&&struts>=35){
if(average>=35 && average<60)
{
alert("Grade C");
}else if(average>=60 && average<80)
{
alert("Grade B");
}else {
alert("Grade A");
}
}else {
alert("Fail");
}
14. switch-Statement
Script.js
var num = prompt("Enter Number","Enter between 1 and 3");
switch(parseInt(num))
{
case 1 : alert("You entered number One");break;
Java Training Center
(No 1 in Training & Placement)
7
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
case 2 : alert("You entered number Two");break;
case 3 : alert("You entered number Three");break;
default : alert("no match");
}
15.for loop
Script.js
function display(){
alert("Welcome to function");
}
for(var i=0;i<4;i++){
display();
}
15.While Loop
Script.js
var num = parseInt(prompt("Enter Number"));
var i=0;
var sum=0;
while(i<=num)
{
sum= sum + i;
i=i+1;
//i++;
}
alert("The Total is "+sum);
16.do-While-Loop
Script.js
var i=5;
do {
alert("number is "+i);
}while(i<4)
17.Break
Script.js
var i;
for (i=0;i<=10;i++)
{
Java Training Center
(No 1 in Training & Placement)
8
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
if (i==3)
{
//continue;
break;
}
document.write("The number is " + i);
document.write("<br>");
}
18.Continue
Script.js
var i=0
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
document.write("The number is " + i);
document.write("<br />");
}
19.Function
Script.html
<html>
<head>
<title>20.a Functions</title>
<script type="text/javascript" src="script.js">
</script></head>
<body><form>
<input type="button" value="click Here" onClick="display()">
</form></body>
</html>
Script.js
function display(){
alert("Welcome to Function");
}
display(); display();
display(); display();
display();
Java Training Center
(No 1 in Training & Placement)
9
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
/* string "name" default it considers as a string*/
/*function without arguments*/
20.Function
HTML AS Above
Script.js
/*function with arguments*/
var i;
//global variable
function display(str,num){
//var i;
/*local variable*/
for(i=0;i<=num;i++){
document.write(str);
}
}
display('x',10);
document.write("<br>");
display('y',20);
21.Function
Script.html
<html><head>
<title>20.d Functions</title>
<script type="text/javascript" src="script.js">
</script></head>
<body><form>
<input type="button" value="click Here" onClick="` (mul(4,6))">
</form></body></html>
Script.js
function mul(num1,num2)
{
var num3=num1*num2;
return num3
}
var result=mul(4,5)+mul(2,3);
alert(result);
alert(typeof(mul));
var res="cfhfgh";
alert(typeof(res));
var x=10;
Java Training Center
(No 1 in Training & Placement)
10
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
alert(typeof(x));
22. function arg
Script.js
function test()
{
alert("No of Args "+arguments.length);
for(i=0;i<arguments.length;i++)
//index generally starts from 0 but here index (.length) starts from 1
{
alert(arguments[i]);
}
}
test(1.2,2.4,3.6,4);
23. Function
function drawLine(str,num)
{
for(var i=0;i<num;i++)
{
document.write(str);
}
if(arguments.length==1){
for(var i=0;i<10;i++)
{
document.write(str);
}
}
}
drawLine('Y');
document.write("<br>");
drawLine('X',20)
24.Object
Script.js
var x="Hello World!";
document.write(x.length+"<br>");
document.write(x.indexOf("H"));
Script3.js
var x="Hello World!";
Java Training Center
(No 1 in Training & Placement)
11
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
document.write(x.length+"<br>");
/*index starts from 1*/
document.write(x.indexOf("H")+"<br>");
document.write(x.indexOf("l")+"<br>");
document.write(x.lastIndexOf("l"));
/*index starts from 0*/
25. lower case and Upper Case
Script.js
var str="Hello world!";
document.write(str.toLowerCase() + "<br>");
document.write(str.toUpperCase());
26.String Match
Script.js
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
27.String Style
var txt = "Hello World!";
document.write("<p>Big: "+txt.big()+"</p>");
document.write("<p>Small: "+txt.small()+"</p>");
document.write("<p>Bold: "+txt.bold()+"</p>");
document.write("<p>Italic: "+txt.italics()+"</p>");
document.write("<p>Fixed: "+txt.fixed()+"</p>");
document.write("<p>Strike: "+txt.strike()+"</p>");
document.write("<p>Fontcolor: "+txt.fontcolor("green")+"</p>");
document.write("<p>Fontsize: "+txt.fontsize(3)+"</p>");
document.write("<p>Subscript: "+txt.sub()+"</p>");
document.write("<p>Superscript: "+txt.sup()+"</p>");
document.write("<p>Link:" +txt.link("http://www.jtcindia.org")+"</p>");
document.write("<p>Blink: "+txt.blink()+" (does not work in IE, Chrome and Safari -
(firefox older version browser))</p>");
28.String Replace
var str="Visit Oracle!";
document.write(str.replace("Oracle","jtcindia")+"<br>");
document.write(str.replace("a","p")+"<br>");
29. Boolean
Java Training Center
(No 1 in Training & Placement)
12
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
var b1=new Boolean(0);
var b2=new Boolean(1);
var b3=new Boolean("");
var b4=new Boolean(null);
var b5=new Boolean(NaN);//not a number
var b6=new Boolean("jtcindia");
document.write("0 is boolean "+ b1 +"<br>");
document.write("1 is boolean "+ b2 +"<br>");
document.write("An empty string is boolean "+b3+"<br>");
document.write("null is boolean "+ b4+ "<br>");
document.write("NaN is boolean "+ b5 +"<br>");
document.write("The string 'jtcindia' is boolean "+b6+"<br>");
30. Boolean
Script.js
var str="Hello world!";
document.write(str.indexOf("d") + "<br>");
document.write(str.indexOf("o") + "<br>");
document.write(str.indexOf("WORLD") + "<br>");
document.write(str.indexOf("HELLO") + "<br>");
document.write(str.indexOf("world")+ "<br>");
document.write(str.indexOf("l")+ "<br>");
document.write(str.lastIndexOf("l"));
/*index starts from 0 in case of indexOf*/
31.Math.round / Math.floor
Script.js
document.write(Math.floor(2.90) + "<br>");
document.write(Math.ceil(2.90) + "<br>");
document.write(Math.round(2.90) + "<br><br>");
document.write(Math.round(2.40) + "<br>");
document.write(Math.ceil(2.40) + "<br>");
document.write(Math.floor(2.40) + "<br><br>");
document.write(Math.floor(2.50) + "<br>");
document.write(Math.ceil(2.50) + "<br>");
document.write(Math.round(2.50) + "<br><br>");
32.math.random
Script.js
Java Training Center
(No 1 in Training & Placement)
13
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
//return a random number between 0 and 1
document.write(Math.random());
document.write("<br>");
//return a random number between 0 and 11
document.write(Math.round(Math.random()*11));
document.write("<br>");
//return a random number between 0 and 10
document.write(Math.floor(Math.random()*11));
33. math.max / math.min
var a=10;
var b=30;
document.write(Math.min(a,18) + "<br>");
document.write(Math.min(5,10) + "<br>");
document.write(Math.max(0,150,30,20,38) + "<br>");
document.write(Math.max(-5,10) + "<br>");
document.write(Math.min(-5,-10) + "<br>");
document.write(Math.max(1.5,2.5));
document.write(Math.min(5,10) + "<br />");
document.write(Math.min(0,150,30,20,38) + "<br />");
document.write(Math.min(-5,10) + "<br />");
document.write(Math.min(-5,-10) + "<br />");
document.write(Math.min(1.5,2.5));
34. Math.sqrt / Math.PI
<html>
<head>
<title>31.Math.sqrt / Math.PI</title>
<script type="text/javascript">
var x=Math.PI;
var y=Math.sqrt(16);
alert(x);
alert(y);
</script></head>
<body>
</body></html>
35.Navigator Object
Script.html
Java Training Center
(No 1 in Training & Placement)
14
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
<html>
<body>
<div id="example" style="background-color:#F60">vghvfgf </div>
<script type="text/javascript">
var txt;
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
document.getElementById("example").innerHTML=txt;
</script>
</body></html>
Script.js
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
document.getElementById("example").innerHTML=txt;
36.timeout
jquery-1.6.4.min
Script.js
<html><head><title>Timeout</title>
<script type="text/javascript">
function time()
{
setTimeout("alertmsg()",3000);
}
function alertmsg()
{
alert("Hello");
}
</script></head><body>
<form>
<input type="button" value="Display alert box in 3 seconds" onClick="time()">
</form></body>
</html>
Java Training Center
(No 1 in Training & Placement)
15
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
37.Start_timer
Script.html
<html><head>
<script type="text/javascript">
var c=0;
var timer_is_on=0;
function timedCount()
{
document.form.samp.value=c;
//document.getElementById("txt").value=c;
c=c+1;
setTimeout("timedCount()",10);
}
function doTimer()
{
if(!timer_is_on)
{
timer_is_on=1;
timedCount();
}}</script></head><body>
<form name="form">
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt" name="samp" value="">
</form>
<p>Click on the button above. The input field will count forever, starting at 0.</p>
</body>
</html>
Script.js
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
document.getElementById("example").innerHTML=txt;
38.Start_timer
Script.html
Java Training Center
(No 1 in Training & Placement)
16
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
<html><head><script type="text/javascript">
function timedText()
{
setTimeout("document.getElementById('txt').value='1 seconds!'",1000);
setTimeout("document.getElementById('txt').value='4 seconds!'",4000);
setTimeout("document.getElementById('txt').value='60 seconds!'",6000);
}</script></head><body>
<form>
<input type="button" value="Display timed!" onClick="timedText()">
<input type="text" id="txt" value="" />
</form>
<p>Click on the button above. The input field will tell you when first,
`four, and six seconds have passed.</p></body></html>
Script.js
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
document.getElementById("example").innerHTML=txt;
39.Start_stop
Script.html
<html><head>
<script type="text/javascript">
var c=0; var t; var timer_is_on=0;
function timedCount()
{ document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
function doTimer()
{ if(!timer_is_on)
{
timer_is_on=1;
//c=0;
timedCount();
}
}
function stopCount()
{
clearTimeout(t);
Java Training Center
(No 1 in Training & Placement)
17
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
timer_is_on=0;
}
</script></head><body>
<form>
<input type="button" value="Start count!" onClick="doTimer()">
<input type="text" id="txt" />
<input type="button" value="Stop count!" onClick="stopCount()">
</form></body></html>
Script.js
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
document.getElementById("example").innerHTML=txt;
40.Array
a. Arrayconcat.html
<html><head><script>
var arr = new Array(1,4,4,5,1,7,10);
alert(arr);
var arr1=arr.concat(11,56,30);
alert(arr1);
var arr2=arr1.concat(70,40,20);
alert(arr2);
</script></head>
</html>
b.ArrayEx.html
<html>
<head>
<script type="text/javascript">
var arr = new Array(1,2,3);
alert(arr);
var j=arr.join('-');
alert(j);
var j1=arr.join(10);
alert(j1);
var r=arr.reverse();
Java Training Center
(No 1 in Training & Placement)
18
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
alert(r);</script></head>
</html>
c.ArraySlice.html
<html>
<head>
<title>Array Slicing</title>
<script type="text/javascript">
var arr = new Array(1,2,4,5,6,7,8,9,4,7,4);
alert(arr);
alert(arr[0]);
var s= arr.slice(3);
alert(s);
alert(arr);
var s1= arr.slice(1,7);
alert(s1);
var s2= arr.slice(3,11);
alert(s2);</script></head>
</html>
d. ArraySort.html
<html><head>
<title>Array Sorting</title>
<script>
var arr = new Array("Som","Mahesh","Manish");
alert(arr);
arr.sort();
alert(arr);
var arr1 = new Array(15,111,132);
arr1.sort();
alert(arr1);
var arr2 = new Array("15","111","132");
arr2.sort();
alert(arr2);
</script></head>
<body></body>
</html>
e. JavaScript array random
<html>
Java Training Center
(No 1 in Training & Placement)
19
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
<head>
<title>JavaScript array random</title>
<script type="text/javascript">
var arr = new Array(10);
arr[0] = "Amit";arr[1] = "Santosh";arr[2] = "Rose";
arr[3] = "India";arr[4] = "News";arr[5] = "Track";
arr[6] = "Sandeep";arr[7] = "Suman";arr[8] = "Saurabh";
arr[9] = "Vineet";
var first=Math.round(Math.random()*9);
document.write("First Name : "+arr[first]+"<br>");
var second=Math.round(Math.random()*9);
document.write("Second Name : "+arr[second]+"<br>");
var third=Math.round(Math.random()*9);
document.write("Third Name : "+arr[third]+"<br>");
</script>
</head>
<body><form>
<h2>JavaScript array Random Example</h2>
<input type="submit" value="Refresh">
</form>
</body>
</html>
Date:
Date.html
<html>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d);
</script>
</body>
</html>
Date1.html
<html>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d.getTime() + " milliseconds since 1901/01/01"+"<br>");
</script>
Java Training Center
(No 1 in Training & Placement)
20
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
</body>
</html>
-----------------------
Date2.html
<html><body><script type="text/javascript">
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
document.write(d.getDay()+"<br>");
document.write("Today is "+weekday[d.getDay()]);
</script></body>
</html>
dateEx.html
<html><body><script type="text/JavaScript">
var d = new Date();
var nowHour = d.getHours();
var nowMinute = d.getMinutes();
var nowSecond = d.getSeconds();
var greeting;
if (nowHour < 12)
{
greeting = "Good Morning";
}
else if (nowHour < 17)
{
greeting = "Good Afternoon";
}
else
{
greeting = "Good Evening";
}
document.write("<h4>" + greeting + " and welcome to my website</h4>");
document.write("According to your clock the time is ");
document.write(nowHour + ":" + nowMinute + ":" + nowSecond);
Java Training Center
(No 1 in Training & Placement)
21
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
</script>
</body>
</html>
dateEx1.html
<html>
<body>
<script type="text/javascript">
var months = new
Array("January","February","March","April","May","June","July","August","September","Oct
ober","November","December");
var d = new Date();
var nowMonth=d.getMonth();
var monthNow = months[nowMonth];
var yearNow = d.getFullYear();
var dayNow = d.getDate();
var daySuffix;
switch(dayNow)
{
case 1:
case 21:
case 31:
daySuffix = "st";
break;
case 2:
case 22:
daySuffix = "nd";
break;
case 3:
case 23:
daySuffix = "rd";
break;
default:
daySuffix = "th";
break;
}
document.write("It is the " + dayNow + daySuffix + " day ");
document.write("in the month of " + monthNow);
document.write(" in the year of " + yearNow);
</script></body>
</html>
Java Training Center
(No 1 in Training & Placement)
22
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Errors:
Cookie.html
<html>
<head>
<script type="text/javascript">
function WriteCookie()
{
if( document.myform.customer.value == "" ){
alert("Enter some value!");
return;
}
cookievalue= document.myform.customer.value + ";";
document.cookie="name=" + cookievalue;
alert("Setting Cookies : " + "name=" + cookievalue );
}
</script>
</head>
<body>
<form name="myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
</form></body></html>
Runtime.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
window.printme();
//syntax is correct but the method is non existed
</script>
</head>
<body></body>
</html>
Java Training Center
(No 1 in Training & Placement)
23
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Syntax.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
window.print(;
</script>
</head><body>
</body></html>
Form:
Pr1.html
<html>
<head>
<script language="javascript">
function greet()
{
var x;
x=f1.sname.value;
alert("Hello "+x);
document.write("Hello "+x);
}
</script>
</head>
<body>
<form name=f1>
<input type=text name=sname value="Som Prakash" maxlength=15 size=25>
<input type=button value="Click Here" onclick="greet()">
</form></body>
</html>
Pr2.html
<html>
<head>
<script language="javascript">
function calculate()
Java Training Center
(No 1 in Training & Placement)
24
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
{
var x,y,z;
x=prompt("enter a value for x ");
y=prompt("enter a value for y ",67);
z = parseFloat(x) + parseFloat(y);
alert("value of x is "+x);
alert("value of y is "+y);
alert("sum is "+z);
}
</script></head>
<body>
<form name=f1>
<input type=button value="Click Here" onClick="calculate()">
</form></body>
</html>
Pr3.html
<HTML>
<head>
<SCRIPT LANGUAGE=JavaScript>
function show_result()
{
var x=parseInt(f1.n1.value);
var y=parseInt(f1.n2.value);
var z;
var c = parseInt(f1.ope.selectedIndex);
switch (c)
{
case 0: z=x+y;
break;
case 1: z=x-y;
break;
case 2: z=x*y;
break;
case 3: z=x/y;
break;
}
f1.n3.value=z;
}
</SCRIPT>
</head>
<BODY>
<form name=f1>
Java Training Center
(No 1 in Training & Placement)
25
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
First Number : <input type=text name=n1> <br>
Second Number : <input type=text name=n2> <br>
<select name=ope onchange="show_result()">
<option> Addition
<option> Subtraction
<option selected> Multiplication
<option> Division
</select> <br>
Result : <input type=text name=n3> <br>
</form>
</BODY>
</HTML>
Pr4.html
<HTML>
<head>
<SCRIPT LANGUAGE=JavaScript>
function show()
{
var c = parseInt(f1.cla.selectedIndex)+1;
switch (c)
{
case 1:
case 2:
case 3:
case 4:
case 5: document.write("Please go to Primary section"); break;
case 6:
case 7: document.write("Please go to Upper Primary section"); break;
case 8:
case 9:
case 10: document.write("Please go to secondary section"); break;
case 11:
case 12: document.write("Please go to higher secondary section"); break;
default: alert("You have typed invalid class number"); break;
}
}
</SCRIPT>
</head>
<BODY>
<form name=f1>
Select The Class
<select name=cla onchange="show()">
Java Training Center
(No 1 in Training & Placement)
26
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
<option> 1
<option> 2
<option> 3
<option> 4
<option selected> 5
<option> 6
<option> 7
<option> 8
<option> 9
<option> 10
<option> 11
<option> 12
</select>
</form>
</BODY>
</HTML>
Pr5.html
<html>
<head>
<script language="javascript">
function print()
{
var c;
for(c=1;c<=10;c=c+2)
{
document.write("<b>"+c+"</b><br>");
}
}
</script>
</head>
<body>
<input type=button value="click here" onclick="print()">
</body>
</html>
Pr6.html
<html>
<head>
<script language="javascript">
function print_table()
{
Java Training Center
(No 1 in Training & Placement)
27
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
var n=parseInt(f1.num1.value);
var c,p;
document.write("Multiplication Table of "+n+"<br>");
for (c=1;c<=10;c++)
{
p=n*c;
document.write("<b>"+n+" * "+c+" = "+p+"<br></b>");
}
}
</script>
</head><body>
<form name=f1>
Enter a Number :<input type=text name=num1 size=10 maxlength=5> <br>
<input type=button value="Print The Table" onclick="print_table()">
</form></body>
</html>
Script.html
<html>
<head>
<title>parseInt & parseFloat</title>
<script type="text/javascript" src="script.js">
</script></head>
<body></body>
</html>
Script.js
function drawLine(str,num)
{
for(i=0;i<num;i++)
{
document.write(str);
}
if(arguments.length==1)
{
for(i=0;i<10;i++)
{
document.write(str);
}} }
drawLine('-');
document.write("<br>");
Java Training Center
(No 1 in Training & Placement)
28
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
drawLine('x',20);
ButtonEx.html
<html>
<head>
<script type="text/javascript">
function operation(obj1){
if(obj1.value=="Add"){
form1.txtResult.value=parseInt(form1.txtNum1.value)+parseInt(form1.txtNum2.value);
}
else if(obj1.value=="Sub")
{
form1.txtResult.value=parseInt(form1.txtNum1.value)-parseInt(form1.txtNum2.value);
}
else if(obj1.value=="Mul")
{
form1.txtResult.value=parseInt(form1.txtNum1.value)*parseInt(form1.txtNum2.value);
}
else
{
form1.txtResult.value=parseInt(form1.txtNum1.value)/parseInt(form1.txtNum2.value);
} }
</script>
</head>
<body>
<form name="form1">
Enter Num1:
<input type="text" name="txtNum1">
<br>
Enter Num2:
<input type="text" name="txtNum2" value="">
<br>
Result:
<input type="text" name="txtResult" value="">
<br>
<input type="button" name="btnAdd" value="Add" onClick='operation(this);'>
<input type="button" name="btnAdd" value="Sub" onClick='operation(this);'>
<input type="button" name="btnAdd" value="Mul" onClick='operation(this);'>
<input type="button" name="btnAdd" value="Div" onClick='operation(this);'>
</form>
</body>
Java Training Center
(No 1 in Training & Placement)
29
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
</html>
CheckBox.html
<html><h4>CheckBox Example</h4><head>
<script>//var value1,value2;/*global variables*/
function getValue()
{
var value1,value2;
/*local variables*/
if(form1.chk1.checked==true)
{
value1=document.getElementById("na").value;
}
else
{
value1="";
}
if(form1.chk2.checked==true)
{
value2=form1.chk2.value;
}
else
{
value2="";
}
alert(value1+" "+value2);
}
</script>
</head>
<body>
<form name="form1">
<input type="checkbox" name="chk1" id="na" value="c" checked="true">
C
<input type="checkbox" name="chk2" value="c++">
C++
<input type="button" name="btnClick" value="click" onClick="getValue();">
</form></body>
</html>
formValidate.html
<html>
<head>
<script type="text/javascript">
Java Training Center
(No 1 in Training & Placement)
30
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
function validate()
{
if(form1.txtName.value=="" && form1.txtAge.value=="")
{
alert("Enter Name and age");
}
else
{
var test = confirm("do you want submit?");
return test;
}
}
function test(obj1)
{
if(isNaN(obj1.value)==true)
/*is not a number - isNaN*/
{
alert("Enter valid age");
obj1.focus();
}
}
</script>
</head>
<body>
<form name="form1" action="user.php" method="get">
Enter Name
<input type="text" name="txtName" size="12" style="background-color:#f00;" value="">
<br>
Enter Age
<input type="text" name="txtAge" size="12" onblur='test(this)'>
<br>
<input type="submit" onClick="validate()">
</form></body>
</html>
ImgEx.html
<html>
<body>
<img name="img1" src="" border="0" width="600" height="350">
<script type="text/javascript">
Java Training Center
(No 1 in Training & Placement)
31
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
var myImages=new
Array("img/Chrysanthemum.jpg","img/Desert.jpg","img/Hydrangeas.jpg","img/Koala.jpg")
;
//document.write(myImages[2]);
var i=Math.round(Math.random()*3);
document.images['img1'].src=myImages[i];
</script>
</body>
</html>
Mouse.html
<html>
<head>
<script type="text/javascript">
<!--
function over() {
alert("Mouse Over");
}
function out() {
alert("Mouse Out");
}
//-->
</script></head>
<body>
<div onMouseOver="over()" onMouseOut="out()">
<h2> This is inside the division </h2>
</div></body>
</html>
onFocus.html
<html>
<head>
<script type="text/javascript">
function display() {
alert("focus");
}
</script>
</head>
Java Training Center
(No 1 in Training & Placement)
32
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
<body>
<input type="text" onFocus="display()">
</body>
</html>
Radio.html
<html><head><h4>Radio Button</h4>
<script type="text/javascript">
function getValue(obj)
{
var value;
for(var i=0;i<obj.length;i++)
{
if(obj[i].checked==true)
{
value=obj[i].value;
break;
}
} alert(value);
}
</script></head><body>
<form>
<input type="radio" name="gender" value="Byke" checked="true">Byke<!--0-->
<input type="radio" name="gender" value="Car"> Car <!-- 1 -->
<input type="radio" name="gender" value="Scooty"> Scooty <!-- 1 -->
<input type="button" name="btnClick" value="click"
onClick="getValue(this.form.gender);">
</form>
</body>
</html>
SelectEx.html
<html>
<head>
<script type="text/javascript">
function display(object1)
{
//alert(object1.value);
document.bgColor=object1.value;
}
</script>
</head><body>
<form>
Java Training Center
(No 1 in Training & Placement)
33
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Courses:
<select name="sltCourses" onChange="display(this)">
<option value="#ff0000">red</option><!-- 0 -->
<option value="yellow">yellow</option>
<option value="green">green</option>
<option value="skyblue">skyblue</option>
<option value="blue">blue</option>
</select>
<!--<input type="button" value="click" onClick="display(this.form.sltCourses)">-->
</form></body>
</html>
selectEx1.html
<html>
<head>
<script type="text/javascript">
var list="";
function display(object1)
{
for(var i=0;i<object1.length;i++)
{
if(object1.options[i].selected)
{
list += object1.options[i].value;//x+=y; x=x+y
}
}
alert(list);
}
</script>
</head>
<body>
<form>
Courses:
<select name="sltCourses" size="7" multiple="multiple">
<option value='c' selected>C</option>
<option value='c++'>C++</option>
<option value='java' selected>JAVA</option>
<option value='vb'>VB</option>
</select>
<input type="button" value="click" onclick='display(this.form.sltCourses)'>
</form>
</body>
</html>
Java Training Center
(No 1 in Training & Placement)
34
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
Mustinclude
Ex2.html
<html>
<body>
<script language="javascript">
var x=19;
var y=2.5;
var a="19";
var b="2.5";
var z,c;
alert ("value of x is "+x);
alert ("value of y is "+y);
z=x/y;
alert ("total is "+z);
alert ("value of a is "+a);
alert ("value of b is "+b);
c=parseInt(a)/parseInt(b);
alert ("the quotient is "+c);
c=parseInt(a)/parseFloat(b);
alert ("the quotient is "+c);
</script>
</body>
</html>
ParseFloat
Script.html
<html>
<head>
<title>onFocus</title>
<script type="text/javascript">
function test(){
alert("please enter a value");
}
</script>
</head><body>
<input type="text" onFocus="test()">
</body></html>
Script.js
document.write(parseInt(10.5)+"<br>");
Java Training Center
(No 1 in Training & Placement)
35
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
document.write(parseInt(10.0)+"<br>");
document.write(parseFloat(10.0)+"<br>");
document.write(parseFloat(10.5)+"<br>");
Ex1.html
<html>
<head>
<script language='JavaScript'>
window.document.write("Example1");
</script></head>
</html>
windowEx.html
<html>
<script>
var newWin;
function newWindow()
{
newWin =
window.open("ex1.html","testWindow","width=400,height=400,status=yes,resizable=yes,
menubar=yes,location=yes");
newWin.document.write("This is New Window");
}
function test()
{
alert("Unload");
newWin.close();
}
</script>
<body onClick='newWindow()' onUnload='test()'>
</body>
</html>
PageRedirect
<html>
<head>
<title>Page redirect</title>
<script type="text/javascript">
function redirect(){
window.location="http://www.jtcindia.org";
}
</script></head>
Java Training Center
(No 1 in Training & Placement)
36
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
<body onLoad="redirect()">
<p>Welcome to index page</p>
</body></html>
PostIncrement
<!DOCTYPE html><html>
<head>
<title>Post Increment</title>
</head>
<body>
<p>Given that y=5, calculate x=y++, and display the result.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=y++;//x=5;y=6
var demoP=document.getElementById("demo")
demoP.innerHTML="x=" + x + ", y=" + y;
}
</script>
<p><strong>Note:</strong> Both variables, x and y, are affected.</p>
</body>
</html>
PreIncrement
<!DOCTYPE html>
<html>
<body>
<p>Given that y=5, calculate x=++y, and display the result.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=++y;//x=y=6
var test=document.getElementById("demo")
test.innerHTML="x=" + x + ", y=" + y;
}
Java Training Center
(No 1 in Training & Placement)
37
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
</script>
<p><strong>Note:</strong> Both variables, x and y, are affected.</p>
</body>
</html>
Catch.html
<!DOCTYPE html><html><body>
<script>
function myFunction()
{
var y=document.getElementById("mess");
y.innerHTML="";
try
{
var x=document.getElementById("demo").value;
if(x=="") throw "empty";
if(isNaN(x)) throw "not a number";
if(x>10) throw "too high";
if(x<5) throw "too low";
}
catch(err)
{
y.innerHTML="Error: " + err + ".";
}
}
</script>
<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<input type="button" onclick="myFunction()" value="Test Input">
<p id="mess"></p>
</body></html>
Try-catch
<!DOCTYPE html><html><head><script>
var txt="";
function message()
{
try
{
adddlert("Welcome guest!");
}
Java Training Center
(No 1 in Training & Placement)
38
JavaScript
Author: Som Prakash Rai www.jtcindia.org
Youtube.com/javatrainingcenterjtc Copyright©JTC
catch(err)
{
txt="There was an error on this page.nn";
txt+="Error description: " + err.message + "nn";
txt+="Click OK to continue.nn";
alert(txt);
}
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()">
</body>
</html>

More Related Content

PDF
EJB Part-1
PDF
Core java introduction
PDF
Jdbc Complete Notes by Java Training Center (Som Sir)
PDF
Bea weblogic job_interview_preparation_guide
PDF
PDF
Java interview questions
PDF
DataFX 8 (JavaOne 2014)
EJB Part-1
Core java introduction
Jdbc Complete Notes by Java Training Center (Som Sir)
Bea weblogic job_interview_preparation_guide
Java interview questions
DataFX 8 (JavaOne 2014)

What's hot (20)

PDF
Java Interview Questions and Answers | Spring and Hibernate Interview Questio...
PPSX
Spring - Part 3 - AOP
PDF
Java j2ee interview_questions
ODP
Java 9 Features
PDF
JavaFX Enterprise (JavaOne 2014)
PPT
Java interview-questions-and-answers
PDF
Hibernate Advance Interview Questions
PDF
Core java interview questions
PPTX
Dev labs alliance top 20 basic java interview question for sdet
PDF
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
PDF
Java 10 New Features
PDF
Test Driven Development with JavaFX
PDF
Efficient JavaScript Unit Testing, JavaOne China 2013
PDF
50+ java interview questions
PPTX
[AnDevCon 2016] Mutation Testing for Android
DOCX
Hibernate3 q&a
PDF
Best interview questions
PPTX
Apache Cordova In Action
PPTX
Dynamic Groovy Edges
DOCX
Hibernate notes
Java Interview Questions and Answers | Spring and Hibernate Interview Questio...
Spring - Part 3 - AOP
Java j2ee interview_questions
Java 9 Features
JavaFX Enterprise (JavaOne 2014)
Java interview-questions-and-answers
Hibernate Advance Interview Questions
Core java interview questions
Dev labs alliance top 20 basic java interview question for sdet
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Java 10 New Features
Test Driven Development with JavaFX
Efficient JavaScript Unit Testing, JavaOne China 2013
50+ java interview questions
[AnDevCon 2016] Mutation Testing for Android
Hibernate3 q&a
Best interview questions
Apache Cordova In Action
Dynamic Groovy Edges
Hibernate notes
Ad

Similar to Java script Examples by Som (20)

PPT
PPT
Java script
PPT
Lecture 4.javascriptpractcieandtheoryppt
PPT
JS-Lecture Lecture PPT contains Control Structures DOM
PPT
Introduction to java script Lecture 4.ppt
PPT
FSJavaScript.ppt
PPTX
wp-UNIT_III.pptx
PPT
Introduction to Scripting programming Language.
PPT
Javascript for beggineers Lecture 4 (1).ppt
PPTX
First javascript workshop : first basics
PPTX
Java script.pptx v
PPTX
BITM3730 10-17.pptx
PDF
Iwt note(module 2)
PPTX
Lecture 5 javascript
PPTX
1. java script language fundamentals
PPTX
An Introduction to JavaScript
PDF
javascriptPresentation.pdf
DOC
Introduction to java script
PPTX
Introduction to JAVA SCRIPT USING HTML and CSS
PDF
Intro to JavaScript - Thinkful LA
Java script
Lecture 4.javascriptpractcieandtheoryppt
JS-Lecture Lecture PPT contains Control Structures DOM
Introduction to java script Lecture 4.ppt
FSJavaScript.ppt
wp-UNIT_III.pptx
Introduction to Scripting programming Language.
Javascript for beggineers Lecture 4 (1).ppt
First javascript workshop : first basics
Java script.pptx v
BITM3730 10-17.pptx
Iwt note(module 2)
Lecture 5 javascript
1. java script language fundamentals
An Introduction to JavaScript
javascriptPresentation.pdf
Introduction to java script
Introduction to JAVA SCRIPT USING HTML and CSS
Intro to JavaScript - Thinkful LA
Ad

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Institutional Correction lecture only . . .
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Lesson notes of climatology university.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Institutional Correction lecture only . . .
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
Lesson notes of climatology university.
102 student loan defaulters named and shamed – Is someone you know on the list?
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
A systematic review of self-coping strategies used by university students to ...
Chinmaya Tiranga quiz Grand Finale.pdf

Java script Examples by Som

  • 1. Java Training Center (No 1 in Training & Placement) 1 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Java Training Center (No.1 in Training & Placement) Javascript Author SomPrakashRai
  • 2. Java Training Center (No 1 in Training & Placement) 2 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Welcome Example(Using Internal Script) <html> <head> <title>1.Welcome to Javascript</title> <script type="text/javascript"> document.write('welcome to javascript'); </script> </head><body> </body></html> Java Script Using External Script. <html> <head> <title>2.External Javascript</title> <script type="text/javascript" src="js/script.js"> </script> </head><body> </body></html> script.js document.write("Welcome to JavaScript from external by Java Training Center"); 3.No-Script <html> <head> <title>3.No Script By Java Traninig Center</title> <script type="text/javascript" src="script.js"> </script> </head><body> <noscript><h2>Sorry, this page requires JavaScript.</h2> </noscript></body> </html> Script.js document.write("Welcome to JavaScript from external by Java Training Center"); 4.Variable <html> <head> <title>2.External Javascript</title> <script type="text/javascript" src="script.js"> </script></head> <body></body> </html>
  • 3. Java Training Center (No 1 in Training & Placement) 3 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Script.js var x=10; var y=20; var z=x+y; var name="JTCNOIDA"; /*we can store the value*/ document.write("the value of x is "+x); document.write("<br>"); document.write("the value of y is "+y); document.write("<br>"); document.write("the value of z is "+z); document.write("<br>"); document.write(name+" is a professional training institute for Java "); 5.arithmetic_operators <html> <head> <title>2.External Javascript by Java Training Center</title> <script type="text/javascript" src="script.js"> </script> </head> <body> <!-- --> </body> </html> Script.js var x=10; var y=20; var z=x/y; var name="JTCNOIDA"; document.write("The value of x is "+x); document.write("<br>"); document.write("The value of y is "+y); document.write("<br>"); document.write("The value of z is "+z); document.write("<br>"); document.write(name+" is a Professional training institute"); 6. arithmetic_operators <html> <head> <title>5.Assignment Operators BY JAVA TRAINING CENTER</title> <script type="text/javascript" src="script.js"> </script> </head><body>
  • 4. Java Training Center (No 1 in Training & Placement) 4 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC </body></html> Scprit.js var x=10; var y=20; document.write("The value of x is "+x); document.write("<br>"); document.write("Operator Test by JTC"); document.write("<br>"); var z=x+=y; //z=x=x+y //var z=x/=y; //var z=x%=y; //x+=y ; x=x+y; document.write("The value of x is "+x); document.write("<br>"); document.write("The value of y is "+y); document.write("<br>"); document.write("The value of z is "+z); 7.+operator_on_string <html> <head> <title>7.+ Operator on String by Java Training Center</title> <script type="text/javascript" src="script.js"> </script></head><body> </body></html> Script.js var x=10; var y=20; var a="10"; var b="20"; document.write("The value for adding two strings a + b = "+a+b); document.write("<br>"); document.write("The value for adding number and string x + b = "+x+b); 8. Alert <html> <head> <title>8.Alert boxes by Example By JAVA TRAINING CENTER</title> <script type="text/javascript" src="script.js">
  • 5. Java Training Center (No 1 in Training & Placement) 5 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC </script></head> <body></body> </html> Script.js alert("Click OK to Proceed"); 9.Confirm <html> <head> <title>8.Confirm box by Java Training Center</title> <script type="text/javascript" src="script.js"> </script></head> <body></body> </html> Script.js confirm("Click OK or Cancel"); 10.Prompt As Above (Html) Script.jsp var x=prompt("Enter the number"); alert("The Value that u entered is "+x); /*prompt considers string value*/ 11.if –Statement Script.js var x = prompt("Enter number 10"); /*string*/ alert(typeof(x)); if(x==="10") { alert("Its Correct"); } else { alert("Its False"); } 12.if-else –Statement. Script.js var num1 = prompt("Enter num1","10"); var num2 = prompt("Enter num2","20"); var num3 = parseInt(num1);
  • 6. Java Training Center (No 1 in Training & Placement) 6 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC var num4 = parseInt(num2); /*parseInt - converting string to integer*/ if(num3 > num4 ) { alert( num3 +" is Bigger"); } else { alert( num4 +" is Bigger"); } 13.if –else –if Statement. Script.js var corejava = parseInt(prompt("Enter ur marks in Core Java")); var advancejava = parseInt(prompt("Enter ur marks in Advance Java ")); var struts = parseInt(prompt("Enter ur marks in Struts")); var total=corejava+advancejava+struts; var average = total/3; alert("average is "+average); //logical operators //&& (AND ) - all the conditions should satisfy //|| (OR) - atleast one conditions should satisfy if(corejava>=35&&advancejava>=35&&struts>=35){ if(average>=35 && average<60) { alert("Grade C"); }else if(average>=60 && average<80) { alert("Grade B"); }else { alert("Grade A"); } }else { alert("Fail"); } 14. switch-Statement Script.js var num = prompt("Enter Number","Enter between 1 and 3"); switch(parseInt(num)) { case 1 : alert("You entered number One");break;
  • 7. Java Training Center (No 1 in Training & Placement) 7 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC case 2 : alert("You entered number Two");break; case 3 : alert("You entered number Three");break; default : alert("no match"); } 15.for loop Script.js function display(){ alert("Welcome to function"); } for(var i=0;i<4;i++){ display(); } 15.While Loop Script.js var num = parseInt(prompt("Enter Number")); var i=0; var sum=0; while(i<=num) { sum= sum + i; i=i+1; //i++; } alert("The Total is "+sum); 16.do-While-Loop Script.js var i=5; do { alert("number is "+i); }while(i<4) 17.Break Script.js var i; for (i=0;i<=10;i++) {
  • 8. Java Training Center (No 1 in Training & Placement) 8 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC if (i==3) { //continue; break; } document.write("The number is " + i); document.write("<br>"); } 18.Continue Script.js var i=0 for (i=0;i<=10;i++) { if (i==3) { continue; } document.write("The number is " + i); document.write("<br />"); } 19.Function Script.html <html> <head> <title>20.a Functions</title> <script type="text/javascript" src="script.js"> </script></head> <body><form> <input type="button" value="click Here" onClick="display()"> </form></body> </html> Script.js function display(){ alert("Welcome to Function"); } display(); display(); display(); display(); display();
  • 9. Java Training Center (No 1 in Training & Placement) 9 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC /* string "name" default it considers as a string*/ /*function without arguments*/ 20.Function HTML AS Above Script.js /*function with arguments*/ var i; //global variable function display(str,num){ //var i; /*local variable*/ for(i=0;i<=num;i++){ document.write(str); } } display('x',10); document.write("<br>"); display('y',20); 21.Function Script.html <html><head> <title>20.d Functions</title> <script type="text/javascript" src="script.js"> </script></head> <body><form> <input type="button" value="click Here" onClick="` (mul(4,6))"> </form></body></html> Script.js function mul(num1,num2) { var num3=num1*num2; return num3 } var result=mul(4,5)+mul(2,3); alert(result); alert(typeof(mul)); var res="cfhfgh"; alert(typeof(res)); var x=10;
  • 10. Java Training Center (No 1 in Training & Placement) 10 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC alert(typeof(x)); 22. function arg Script.js function test() { alert("No of Args "+arguments.length); for(i=0;i<arguments.length;i++) //index generally starts from 0 but here index (.length) starts from 1 { alert(arguments[i]); } } test(1.2,2.4,3.6,4); 23. Function function drawLine(str,num) { for(var i=0;i<num;i++) { document.write(str); } if(arguments.length==1){ for(var i=0;i<10;i++) { document.write(str); } } } drawLine('Y'); document.write("<br>"); drawLine('X',20) 24.Object Script.js var x="Hello World!"; document.write(x.length+"<br>"); document.write(x.indexOf("H")); Script3.js var x="Hello World!";
  • 11. Java Training Center (No 1 in Training & Placement) 11 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC document.write(x.length+"<br>"); /*index starts from 1*/ document.write(x.indexOf("H")+"<br>"); document.write(x.indexOf("l")+"<br>"); document.write(x.lastIndexOf("l")); /*index starts from 0*/ 25. lower case and Upper Case Script.js var str="Hello world!"; document.write(str.toLowerCase() + "<br>"); document.write(str.toUpperCase()); 26.String Match Script.js var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); 27.String Style var txt = "Hello World!"; document.write("<p>Big: "+txt.big()+"</p>"); document.write("<p>Small: "+txt.small()+"</p>"); document.write("<p>Bold: "+txt.bold()+"</p>"); document.write("<p>Italic: "+txt.italics()+"</p>"); document.write("<p>Fixed: "+txt.fixed()+"</p>"); document.write("<p>Strike: "+txt.strike()+"</p>"); document.write("<p>Fontcolor: "+txt.fontcolor("green")+"</p>"); document.write("<p>Fontsize: "+txt.fontsize(3)+"</p>"); document.write("<p>Subscript: "+txt.sub()+"</p>"); document.write("<p>Superscript: "+txt.sup()+"</p>"); document.write("<p>Link:" +txt.link("http://www.jtcindia.org")+"</p>"); document.write("<p>Blink: "+txt.blink()+" (does not work in IE, Chrome and Safari - (firefox older version browser))</p>"); 28.String Replace var str="Visit Oracle!"; document.write(str.replace("Oracle","jtcindia")+"<br>"); document.write(str.replace("a","p")+"<br>"); 29. Boolean
  • 12. Java Training Center (No 1 in Training & Placement) 12 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC var b1=new Boolean(0); var b2=new Boolean(1); var b3=new Boolean(""); var b4=new Boolean(null); var b5=new Boolean(NaN);//not a number var b6=new Boolean("jtcindia"); document.write("0 is boolean "+ b1 +"<br>"); document.write("1 is boolean "+ b2 +"<br>"); document.write("An empty string is boolean "+b3+"<br>"); document.write("null is boolean "+ b4+ "<br>"); document.write("NaN is boolean "+ b5 +"<br>"); document.write("The string 'jtcindia' is boolean "+b6+"<br>"); 30. Boolean Script.js var str="Hello world!"; document.write(str.indexOf("d") + "<br>"); document.write(str.indexOf("o") + "<br>"); document.write(str.indexOf("WORLD") + "<br>"); document.write(str.indexOf("HELLO") + "<br>"); document.write(str.indexOf("world")+ "<br>"); document.write(str.indexOf("l")+ "<br>"); document.write(str.lastIndexOf("l")); /*index starts from 0 in case of indexOf*/ 31.Math.round / Math.floor Script.js document.write(Math.floor(2.90) + "<br>"); document.write(Math.ceil(2.90) + "<br>"); document.write(Math.round(2.90) + "<br><br>"); document.write(Math.round(2.40) + "<br>"); document.write(Math.ceil(2.40) + "<br>"); document.write(Math.floor(2.40) + "<br><br>"); document.write(Math.floor(2.50) + "<br>"); document.write(Math.ceil(2.50) + "<br>"); document.write(Math.round(2.50) + "<br><br>"); 32.math.random Script.js
  • 13. Java Training Center (No 1 in Training & Placement) 13 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC //return a random number between 0 and 1 document.write(Math.random()); document.write("<br>"); //return a random number between 0 and 11 document.write(Math.round(Math.random()*11)); document.write("<br>"); //return a random number between 0 and 10 document.write(Math.floor(Math.random()*11)); 33. math.max / math.min var a=10; var b=30; document.write(Math.min(a,18) + "<br>"); document.write(Math.min(5,10) + "<br>"); document.write(Math.max(0,150,30,20,38) + "<br>"); document.write(Math.max(-5,10) + "<br>"); document.write(Math.min(-5,-10) + "<br>"); document.write(Math.max(1.5,2.5)); document.write(Math.min(5,10) + "<br />"); document.write(Math.min(0,150,30,20,38) + "<br />"); document.write(Math.min(-5,10) + "<br />"); document.write(Math.min(-5,-10) + "<br />"); document.write(Math.min(1.5,2.5)); 34. Math.sqrt / Math.PI <html> <head> <title>31.Math.sqrt / Math.PI</title> <script type="text/javascript"> var x=Math.PI; var y=Math.sqrt(16); alert(x); alert(y); </script></head> <body> </body></html> 35.Navigator Object Script.html
  • 14. Java Training Center (No 1 in Training & Placement) 14 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC <html> <body> <div id="example" style="background-color:#F60">vghvfgf </div> <script type="text/javascript"> var txt; txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>"; txt+= "<p>Browser Name: " + navigator.appName + "</p>"; txt+= "<p>Browser Version: " + navigator.appVersion + "</p>"; txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>"; txt+= "<p>Platform: " + navigator.platform + "</p>"; txt+= "<p>User-agent header: " + navigator.userAgent + "</p>"; document.getElementById("example").innerHTML=txt; </script> </body></html> Script.js txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>"; txt+= "<p>Browser Name: " + navigator.appName + "</p>"; txt+= "<p>Browser Version: " + navigator.appVersion + "</p>"; txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>"; txt+= "<p>Platform: " + navigator.platform + "</p>"; txt+= "<p>User-agent header: " + navigator.userAgent + "</p>"; document.getElementById("example").innerHTML=txt; 36.timeout jquery-1.6.4.min Script.js <html><head><title>Timeout</title> <script type="text/javascript"> function time() { setTimeout("alertmsg()",3000); } function alertmsg() { alert("Hello"); } </script></head><body> <form> <input type="button" value="Display alert box in 3 seconds" onClick="time()"> </form></body> </html>
  • 15. Java Training Center (No 1 in Training & Placement) 15 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC 37.Start_timer Script.html <html><head> <script type="text/javascript"> var c=0; var timer_is_on=0; function timedCount() { document.form.samp.value=c; //document.getElementById("txt").value=c; c=c+1; setTimeout("timedCount()",10); } function doTimer() { if(!timer_is_on) { timer_is_on=1; timedCount(); }}</script></head><body> <form name="form"> <input type="button" value="Start count!" onClick="doTimer()"> <input type="text" id="txt" name="samp" value=""> </form> <p>Click on the button above. The input field will count forever, starting at 0.</p> </body> </html> Script.js txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>"; txt+= "<p>Browser Name: " + navigator.appName + "</p>"; txt+= "<p>Browser Version: " + navigator.appVersion + "</p>"; txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>"; txt+= "<p>Platform: " + navigator.platform + "</p>"; txt+= "<p>User-agent header: " + navigator.userAgent + "</p>"; document.getElementById("example").innerHTML=txt; 38.Start_timer Script.html
  • 16. Java Training Center (No 1 in Training & Placement) 16 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC <html><head><script type="text/javascript"> function timedText() { setTimeout("document.getElementById('txt').value='1 seconds!'",1000); setTimeout("document.getElementById('txt').value='4 seconds!'",4000); setTimeout("document.getElementById('txt').value='60 seconds!'",6000); }</script></head><body> <form> <input type="button" value="Display timed!" onClick="timedText()"> <input type="text" id="txt" value="" /> </form> <p>Click on the button above. The input field will tell you when first, `four, and six seconds have passed.</p></body></html> Script.js txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>"; txt+= "<p>Browser Name: " + navigator.appName + "</p>"; txt+= "<p>Browser Version: " + navigator.appVersion + "</p>"; txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>"; txt+= "<p>Platform: " + navigator.platform + "</p>"; txt+= "<p>User-agent header: " + navigator.userAgent + "</p>"; document.getElementById("example").innerHTML=txt; 39.Start_stop Script.html <html><head> <script type="text/javascript"> var c=0; var t; var timer_is_on=0; function timedCount() { document.getElementById('txt').value=c; c=c+1; t=setTimeout("timedCount()",1000); } function doTimer() { if(!timer_is_on) { timer_is_on=1; //c=0; timedCount(); } } function stopCount() { clearTimeout(t);
  • 17. Java Training Center (No 1 in Training & Placement) 17 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC timer_is_on=0; } </script></head><body> <form> <input type="button" value="Start count!" onClick="doTimer()"> <input type="text" id="txt" /> <input type="button" value="Stop count!" onClick="stopCount()"> </form></body></html> Script.js txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>"; txt+= "<p>Browser Name: " + navigator.appName + "</p>"; txt+= "<p>Browser Version: " + navigator.appVersion + "</p>"; txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>"; txt+= "<p>Platform: " + navigator.platform + "</p>"; txt+= "<p>User-agent header: " + navigator.userAgent + "</p>"; document.getElementById("example").innerHTML=txt; 40.Array a. Arrayconcat.html <html><head><script> var arr = new Array(1,4,4,5,1,7,10); alert(arr); var arr1=arr.concat(11,56,30); alert(arr1); var arr2=arr1.concat(70,40,20); alert(arr2); </script></head> </html> b.ArrayEx.html <html> <head> <script type="text/javascript"> var arr = new Array(1,2,3); alert(arr); var j=arr.join('-'); alert(j); var j1=arr.join(10); alert(j1); var r=arr.reverse();
  • 18. Java Training Center (No 1 in Training & Placement) 18 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC alert(r);</script></head> </html> c.ArraySlice.html <html> <head> <title>Array Slicing</title> <script type="text/javascript"> var arr = new Array(1,2,4,5,6,7,8,9,4,7,4); alert(arr); alert(arr[0]); var s= arr.slice(3); alert(s); alert(arr); var s1= arr.slice(1,7); alert(s1); var s2= arr.slice(3,11); alert(s2);</script></head> </html> d. ArraySort.html <html><head> <title>Array Sorting</title> <script> var arr = new Array("Som","Mahesh","Manish"); alert(arr); arr.sort(); alert(arr); var arr1 = new Array(15,111,132); arr1.sort(); alert(arr1); var arr2 = new Array("15","111","132"); arr2.sort(); alert(arr2); </script></head> <body></body> </html> e. JavaScript array random <html>
  • 19. Java Training Center (No 1 in Training & Placement) 19 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC <head> <title>JavaScript array random</title> <script type="text/javascript"> var arr = new Array(10); arr[0] = "Amit";arr[1] = "Santosh";arr[2] = "Rose"; arr[3] = "India";arr[4] = "News";arr[5] = "Track"; arr[6] = "Sandeep";arr[7] = "Suman";arr[8] = "Saurabh"; arr[9] = "Vineet"; var first=Math.round(Math.random()*9); document.write("First Name : "+arr[first]+"<br>"); var second=Math.round(Math.random()*9); document.write("Second Name : "+arr[second]+"<br>"); var third=Math.round(Math.random()*9); document.write("Third Name : "+arr[third]+"<br>"); </script> </head> <body><form> <h2>JavaScript array Random Example</h2> <input type="submit" value="Refresh"> </form> </body> </html> Date: Date.html <html> <body> <script type="text/javascript"> var d=new Date(); document.write(d); </script> </body> </html> Date1.html <html> <body> <script type="text/javascript"> var d=new Date(); document.write(d.getTime() + " milliseconds since 1901/01/01"+"<br>"); </script>
  • 20. Java Training Center (No 1 in Training & Placement) 20 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC </body> </html> ----------------------- Date2.html <html><body><script type="text/javascript"> var d=new Date(); var weekday=new Array(7); weekday[0]="Sunday"; weekday[1]="Monday"; weekday[2]="Tuesday"; weekday[3]="Wednesday"; weekday[4]="Thursday"; weekday[5]="Friday"; weekday[6]="Saturday"; document.write(d.getDay()+"<br>"); document.write("Today is "+weekday[d.getDay()]); </script></body> </html> dateEx.html <html><body><script type="text/JavaScript"> var d = new Date(); var nowHour = d.getHours(); var nowMinute = d.getMinutes(); var nowSecond = d.getSeconds(); var greeting; if (nowHour < 12) { greeting = "Good Morning"; } else if (nowHour < 17) { greeting = "Good Afternoon"; } else { greeting = "Good Evening"; } document.write("<h4>" + greeting + " and welcome to my website</h4>"); document.write("According to your clock the time is "); document.write(nowHour + ":" + nowMinute + ":" + nowSecond);
  • 21. Java Training Center (No 1 in Training & Placement) 21 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC </script> </body> </html> dateEx1.html <html> <body> <script type="text/javascript"> var months = new Array("January","February","March","April","May","June","July","August","September","Oct ober","November","December"); var d = new Date(); var nowMonth=d.getMonth(); var monthNow = months[nowMonth]; var yearNow = d.getFullYear(); var dayNow = d.getDate(); var daySuffix; switch(dayNow) { case 1: case 21: case 31: daySuffix = "st"; break; case 2: case 22: daySuffix = "nd"; break; case 3: case 23: daySuffix = "rd"; break; default: daySuffix = "th"; break; } document.write("It is the " + dayNow + daySuffix + " day "); document.write("in the month of " + monthNow); document.write(" in the year of " + yearNow); </script></body> </html>
  • 22. Java Training Center (No 1 in Training & Placement) 22 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Errors: Cookie.html <html> <head> <script type="text/javascript"> function WriteCookie() { if( document.myform.customer.value == "" ){ alert("Enter some value!"); return; } cookievalue= document.myform.customer.value + ";"; document.cookie="name=" + cookievalue; alert("Setting Cookies : " + "name=" + cookievalue ); } </script> </head> <body> <form name="myform" action=""> Enter name: <input type="text" name="customer"/> <input type="button" value="Set Cookie" onclick="WriteCookie();"/> </form></body></html> Runtime.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> window.printme(); //syntax is correct but the method is non existed </script> </head> <body></body> </html>
  • 23. Java Training Center (No 1 in Training & Placement) 23 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Syntax.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript"> window.print(; </script> </head><body> </body></html> Form: Pr1.html <html> <head> <script language="javascript"> function greet() { var x; x=f1.sname.value; alert("Hello "+x); document.write("Hello "+x); } </script> </head> <body> <form name=f1> <input type=text name=sname value="Som Prakash" maxlength=15 size=25> <input type=button value="Click Here" onclick="greet()"> </form></body> </html> Pr2.html <html> <head> <script language="javascript"> function calculate()
  • 24. Java Training Center (No 1 in Training & Placement) 24 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC { var x,y,z; x=prompt("enter a value for x "); y=prompt("enter a value for y ",67); z = parseFloat(x) + parseFloat(y); alert("value of x is "+x); alert("value of y is "+y); alert("sum is "+z); } </script></head> <body> <form name=f1> <input type=button value="Click Here" onClick="calculate()"> </form></body> </html> Pr3.html <HTML> <head> <SCRIPT LANGUAGE=JavaScript> function show_result() { var x=parseInt(f1.n1.value); var y=parseInt(f1.n2.value); var z; var c = parseInt(f1.ope.selectedIndex); switch (c) { case 0: z=x+y; break; case 1: z=x-y; break; case 2: z=x*y; break; case 3: z=x/y; break; } f1.n3.value=z; } </SCRIPT> </head> <BODY> <form name=f1>
  • 25. Java Training Center (No 1 in Training & Placement) 25 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC First Number : <input type=text name=n1> <br> Second Number : <input type=text name=n2> <br> <select name=ope onchange="show_result()"> <option> Addition <option> Subtraction <option selected> Multiplication <option> Division </select> <br> Result : <input type=text name=n3> <br> </form> </BODY> </HTML> Pr4.html <HTML> <head> <SCRIPT LANGUAGE=JavaScript> function show() { var c = parseInt(f1.cla.selectedIndex)+1; switch (c) { case 1: case 2: case 3: case 4: case 5: document.write("Please go to Primary section"); break; case 6: case 7: document.write("Please go to Upper Primary section"); break; case 8: case 9: case 10: document.write("Please go to secondary section"); break; case 11: case 12: document.write("Please go to higher secondary section"); break; default: alert("You have typed invalid class number"); break; } } </SCRIPT> </head> <BODY> <form name=f1> Select The Class <select name=cla onchange="show()">
  • 26. Java Training Center (No 1 in Training & Placement) 26 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC <option> 1 <option> 2 <option> 3 <option> 4 <option selected> 5 <option> 6 <option> 7 <option> 8 <option> 9 <option> 10 <option> 11 <option> 12 </select> </form> </BODY> </HTML> Pr5.html <html> <head> <script language="javascript"> function print() { var c; for(c=1;c<=10;c=c+2) { document.write("<b>"+c+"</b><br>"); } } </script> </head> <body> <input type=button value="click here" onclick="print()"> </body> </html> Pr6.html <html> <head> <script language="javascript"> function print_table() {
  • 27. Java Training Center (No 1 in Training & Placement) 27 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC var n=parseInt(f1.num1.value); var c,p; document.write("Multiplication Table of "+n+"<br>"); for (c=1;c<=10;c++) { p=n*c; document.write("<b>"+n+" * "+c+" = "+p+"<br></b>"); } } </script> </head><body> <form name=f1> Enter a Number :<input type=text name=num1 size=10 maxlength=5> <br> <input type=button value="Print The Table" onclick="print_table()"> </form></body> </html> Script.html <html> <head> <title>parseInt & parseFloat</title> <script type="text/javascript" src="script.js"> </script></head> <body></body> </html> Script.js function drawLine(str,num) { for(i=0;i<num;i++) { document.write(str); } if(arguments.length==1) { for(i=0;i<10;i++) { document.write(str); }} } drawLine('-'); document.write("<br>");
  • 28. Java Training Center (No 1 in Training & Placement) 28 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC drawLine('x',20); ButtonEx.html <html> <head> <script type="text/javascript"> function operation(obj1){ if(obj1.value=="Add"){ form1.txtResult.value=parseInt(form1.txtNum1.value)+parseInt(form1.txtNum2.value); } else if(obj1.value=="Sub") { form1.txtResult.value=parseInt(form1.txtNum1.value)-parseInt(form1.txtNum2.value); } else if(obj1.value=="Mul") { form1.txtResult.value=parseInt(form1.txtNum1.value)*parseInt(form1.txtNum2.value); } else { form1.txtResult.value=parseInt(form1.txtNum1.value)/parseInt(form1.txtNum2.value); } } </script> </head> <body> <form name="form1"> Enter Num1: <input type="text" name="txtNum1"> <br> Enter Num2: <input type="text" name="txtNum2" value=""> <br> Result: <input type="text" name="txtResult" value=""> <br> <input type="button" name="btnAdd" value="Add" onClick='operation(this);'> <input type="button" name="btnAdd" value="Sub" onClick='operation(this);'> <input type="button" name="btnAdd" value="Mul" onClick='operation(this);'> <input type="button" name="btnAdd" value="Div" onClick='operation(this);'> </form> </body>
  • 29. Java Training Center (No 1 in Training & Placement) 29 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC </html> CheckBox.html <html><h4>CheckBox Example</h4><head> <script>//var value1,value2;/*global variables*/ function getValue() { var value1,value2; /*local variables*/ if(form1.chk1.checked==true) { value1=document.getElementById("na").value; } else { value1=""; } if(form1.chk2.checked==true) { value2=form1.chk2.value; } else { value2=""; } alert(value1+" "+value2); } </script> </head> <body> <form name="form1"> <input type="checkbox" name="chk1" id="na" value="c" checked="true"> C <input type="checkbox" name="chk2" value="c++"> C++ <input type="button" name="btnClick" value="click" onClick="getValue();"> </form></body> </html> formValidate.html <html> <head> <script type="text/javascript">
  • 30. Java Training Center (No 1 in Training & Placement) 30 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC function validate() { if(form1.txtName.value=="" && form1.txtAge.value=="") { alert("Enter Name and age"); } else { var test = confirm("do you want submit?"); return test; } } function test(obj1) { if(isNaN(obj1.value)==true) /*is not a number - isNaN*/ { alert("Enter valid age"); obj1.focus(); } } </script> </head> <body> <form name="form1" action="user.php" method="get"> Enter Name <input type="text" name="txtName" size="12" style="background-color:#f00;" value=""> <br> Enter Age <input type="text" name="txtAge" size="12" onblur='test(this)'> <br> <input type="submit" onClick="validate()"> </form></body> </html> ImgEx.html <html> <body> <img name="img1" src="" border="0" width="600" height="350"> <script type="text/javascript">
  • 31. Java Training Center (No 1 in Training & Placement) 31 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC var myImages=new Array("img/Chrysanthemum.jpg","img/Desert.jpg","img/Hydrangeas.jpg","img/Koala.jpg") ; //document.write(myImages[2]); var i=Math.round(Math.random()*3); document.images['img1'].src=myImages[i]; </script> </body> </html> Mouse.html <html> <head> <script type="text/javascript"> <!-- function over() { alert("Mouse Over"); } function out() { alert("Mouse Out"); } //--> </script></head> <body> <div onMouseOver="over()" onMouseOut="out()"> <h2> This is inside the division </h2> </div></body> </html> onFocus.html <html> <head> <script type="text/javascript"> function display() { alert("focus"); } </script> </head>
  • 32. Java Training Center (No 1 in Training & Placement) 32 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC <body> <input type="text" onFocus="display()"> </body> </html> Radio.html <html><head><h4>Radio Button</h4> <script type="text/javascript"> function getValue(obj) { var value; for(var i=0;i<obj.length;i++) { if(obj[i].checked==true) { value=obj[i].value; break; } } alert(value); } </script></head><body> <form> <input type="radio" name="gender" value="Byke" checked="true">Byke<!--0--> <input type="radio" name="gender" value="Car"> Car <!-- 1 --> <input type="radio" name="gender" value="Scooty"> Scooty <!-- 1 --> <input type="button" name="btnClick" value="click" onClick="getValue(this.form.gender);"> </form> </body> </html> SelectEx.html <html> <head> <script type="text/javascript"> function display(object1) { //alert(object1.value); document.bgColor=object1.value; } </script> </head><body> <form>
  • 33. Java Training Center (No 1 in Training & Placement) 33 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Courses: <select name="sltCourses" onChange="display(this)"> <option value="#ff0000">red</option><!-- 0 --> <option value="yellow">yellow</option> <option value="green">green</option> <option value="skyblue">skyblue</option> <option value="blue">blue</option> </select> <!--<input type="button" value="click" onClick="display(this.form.sltCourses)">--> </form></body> </html> selectEx1.html <html> <head> <script type="text/javascript"> var list=""; function display(object1) { for(var i=0;i<object1.length;i++) { if(object1.options[i].selected) { list += object1.options[i].value;//x+=y; x=x+y } } alert(list); } </script> </head> <body> <form> Courses: <select name="sltCourses" size="7" multiple="multiple"> <option value='c' selected>C</option> <option value='c++'>C++</option> <option value='java' selected>JAVA</option> <option value='vb'>VB</option> </select> <input type="button" value="click" onclick='display(this.form.sltCourses)'> </form> </body> </html>
  • 34. Java Training Center (No 1 in Training & Placement) 34 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC Mustinclude Ex2.html <html> <body> <script language="javascript"> var x=19; var y=2.5; var a="19"; var b="2.5"; var z,c; alert ("value of x is "+x); alert ("value of y is "+y); z=x/y; alert ("total is "+z); alert ("value of a is "+a); alert ("value of b is "+b); c=parseInt(a)/parseInt(b); alert ("the quotient is "+c); c=parseInt(a)/parseFloat(b); alert ("the quotient is "+c); </script> </body> </html> ParseFloat Script.html <html> <head> <title>onFocus</title> <script type="text/javascript"> function test(){ alert("please enter a value"); } </script> </head><body> <input type="text" onFocus="test()"> </body></html> Script.js document.write(parseInt(10.5)+"<br>");
  • 35. Java Training Center (No 1 in Training & Placement) 35 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC document.write(parseInt(10.0)+"<br>"); document.write(parseFloat(10.0)+"<br>"); document.write(parseFloat(10.5)+"<br>"); Ex1.html <html> <head> <script language='JavaScript'> window.document.write("Example1"); </script></head> </html> windowEx.html <html> <script> var newWin; function newWindow() { newWin = window.open("ex1.html","testWindow","width=400,height=400,status=yes,resizable=yes, menubar=yes,location=yes"); newWin.document.write("This is New Window"); } function test() { alert("Unload"); newWin.close(); } </script> <body onClick='newWindow()' onUnload='test()'> </body> </html> PageRedirect <html> <head> <title>Page redirect</title> <script type="text/javascript"> function redirect(){ window.location="http://www.jtcindia.org"; } </script></head>
  • 36. Java Training Center (No 1 in Training & Placement) 36 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC <body onLoad="redirect()"> <p>Welcome to index page</p> </body></html> PostIncrement <!DOCTYPE html><html> <head> <title>Post Increment</title> </head> <body> <p>Given that y=5, calculate x=y++, and display the result.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y++;//x=5;y=6 var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x + ", y=" + y; } </script> <p><strong>Note:</strong> Both variables, x and y, are affected.</p> </body> </html> PreIncrement <!DOCTYPE html> <html> <body> <p>Given that y=5, calculate x=++y, and display the result.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=++y;//x=y=6 var test=document.getElementById("demo") test.innerHTML="x=" + x + ", y=" + y; }
  • 37. Java Training Center (No 1 in Training & Placement) 37 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC </script> <p><strong>Note:</strong> Both variables, x and y, are affected.</p> </body> </html> Catch.html <!DOCTYPE html><html><body> <script> function myFunction() { var y=document.getElementById("mess"); y.innerHTML=""; try { var x=document.getElementById("demo").value; if(x=="") throw "empty"; if(isNaN(x)) throw "not a number"; if(x>10) throw "too high"; if(x<5) throw "too low"; } catch(err) { y.innerHTML="Error: " + err + "."; } } </script> <h1>My First JavaScript</h1> <p>Please input a number between 5 and 10:</p> <input id="demo" type="text"> <input type="button" onclick="myFunction()" value="Test Input"> <p id="mess"></p> </body></html> Try-catch <!DOCTYPE html><html><head><script> var txt=""; function message() { try { adddlert("Welcome guest!"); }
  • 38. Java Training Center (No 1 in Training & Placement) 38 JavaScript Author: Som Prakash Rai www.jtcindia.org Youtube.com/javatrainingcenterjtc Copyright©JTC catch(err) { txt="There was an error on this page.nn"; txt+="Error description: " + err.message + "nn"; txt+="Click OK to continue.nn"; alert(txt); } } </script> </head> <body> <input type="button" value="View message" onclick="message()"> </body> </html>