1. Write a JavaScript that shows how a variable’s type can be changed on-the-fly....
<html>
<body>
<script type="text/javascript">
var a=10;
document.write(typeof(a));
a=12.5;
document.write("<br>");
document.write(typeof(a));
a='c';
document.write("<br>");
document.write(typeof(a));
a=true;
document.write("<br>");
document.write(typeof(a));
a="Chriag";
document.write("<br>");
document.write(typeof(a));
</script>
</body>
</html>
Shared By : Jay Parikh (C.U.Shah Collage of M.C.A.) Show/Hide Program
<html>
<script>
var a,b,c
a=5
b='a'
c=a+b
document.write("Adding number and words : " + c )
document.write("<br>");
a=5
b=10.5
c=a+b
document.write("Adding int and float : " + c )
document.write("<br>");
a=5
b=10.5
c=b/a
document.write("Division float and int : " + c )
</script>
</html>
2. Write a JavaScript that demonstrates the use of operators....
Shared By : Chirag M Daxini (L.J.Institute Of Engineering & Technology) Show/Hide Program
<html>
<body>
<script type="text/javascript">
var a=10;
document.write("Initial Value Of A Is: "+a);
document.write("<br>");
a+=10;
document.write("a+=10 : "+a);
document.write("<br>");
a-=10;
document.write("a-=10 : "+a);
document.write("<br>");
a*=10;
document.write("a*=10 : "+a);
document.write("<br>");
a/=10;
document.write("a/=10 : "+a);
</script>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function fun()
{
document.write("1.Addition</br>");
document.write("2.Subtraction</br>");
document.write("3.Multiplication</br>");
document.write("4.Division</br>");
do
{
var a,b;
a = prompt("Enter value of a:");
b = prompt("Enter value of b:");
a = parseInt(a);
b = parseInt(b);
var ch;
ch = prompt("Enter your choice:"); document.write("</br>.............<b>OUTPUT</b>...........</br>");
switch(parseInt(ch))
{
case 1: document.write("Addition:"+(a+=b)+"</br>");
break;
case 2: document.write("Subtraction:"+(a-=b)+"</br>");
break;
case 3: document.write("Multiplication:"+(a*=b)+"</br>");
break;
case 4: document.write("Division:"+(a/=b)+"</br>");
break;
default:
document.write("Enter proper choice</br>");
}
document.write("Do u want to continue?...Press y....");
var s;
v = prompt("Enter Value:");
}while(v == 'y');
}
</script>
</head>
<body onload="fun()">
</body>
</html>
3. Create a Form in HTML with two fields, minimum and maximum,...
Shared By : Bhavin Machchhar (Sunshine(rajkot) ) Show/Hide Program
<html>
<head>
<script language="javascript">
function minnum(form)
{
var input;
input=document.myform.min.value;
if(parseInt(input)!=input)
{
alert("please enter a numeric value");
document.myform.min.value='';
}
var m1;
m1=document.myform.max.value;
if(parseInt(input)>parseInt(m1))
{
alert("Enter minimum value");
}
}
function maxnum(form)
{
var input1;
input1=document.myform.max.value;
if(parseInt(input1)!=input1)
{
alert("please enter a numeric value");
document.myform.max.value='';
}
}
</script>
</head>
<body>
<form name="myform">
<h4>MAX</h4>
<input type="text" value="" name="max" size=20 onBlur='maxnum(this.form)'>
<h4>MIN</h4>
<input type="text" value="" name="min" size=20 onBlur='minnum(this.form)'>
<br>
</body>
</html>
4. Write a JavaScript that finds out multiples of 10 in 0 to 10000,...
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function func1( )
{
setTimeout ( "doSomething()", 10000 );
}
function doSomething( )
{
var i = 1;
for(i=1;i<10000;i++)
if(i%10==0)
document.write(i + ' , ');
}
</script>
</head>
<body >
<form name=f1>
<input type="button" name="clickMe" value="Start!"
onclick="func1()"/>
</form>
</body>
</html>
5. Write a JavaScript to generate two random numbers...
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function generate()
{
var my_num1=Math.random();
var my_num2=Math.random();
document.write('<br>First Random Number:' + my_num1);
document.write('<br>Second Random Number:' + my_num2);
if (my_num1 < my_num2)
{
document.write('<br><br>Minimum Number:' + my_num1);
document.write('<br>Maximum Number:' + my_num2);
}
else
{
document.write('<br><br>Minimum Number:' + my_num2);
document.write('<br>Maximum Number:' + my_num1);
}
}
</script>
</head>
<body >
<form name=f1>
<input type=button value='Display' onClick='generate()';>
</form>
</body>
</html>
Shared By : DARSHAN PRAJAPATI(KIRC - kalol) Show/Hide Program
<!-- GTU LIST 5 :== Write a JavaScript to generate two random numbers and find out maximum and
minimum out of it -->
<html>
<head>
<script language="Javascript">
function check()
{
var no1=Math.random();//generating 1st random number
var no2=Math.random();//generating 2nd random number
document.write(no1+"\r\n");//printing of 1st number
document.write("\n"+no2);//printing of 2nd number
if(no1>no2)
{
document.write("\nNumber One is Greater!!!\n"+no1);
}
else
{
document.write("\nNumber Two is Greater!!!\n"+no2);
}
}//end of function
</script>
</head>
<body onLoad="check()">
</body>
</html>
6. Write a JavaScript to remove the highest element from the...
<html>
<head>
<script type="text/javascript">
var a=new Array();
function funadd()
{
a.push(parseInt(document.frm.txtdata.value));
document.getElementById("pera").innerHTML=a;
document.frm.txtdata.value="";
document.frm.txtdata.focus();
}
function funremove()
{
var l=a.length;
for(i=0;i<=l;i++)
{
for(j=i+1;j<l;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
alert("The deleted element is: "+a.splice(l-1,1));
document.getElementById("msg").innerHTML="<b><u>Sorted Array:</u></b>";
document.getElementById("sortdata").innerHTML=a;
}
</script>
</head>
<body>
<form name=frm>
<input type="text" id="txtdata">
<input type=button value="ADD" onclick="funadd();">
<input type=button value="REMOVE" onclick="funremove();">
</br>
</br>
<b><u>Elements In Array:</u></b>
</form>
<p id=pera></p>
<p id=Msg></p>
<p id=sortdata></p>
</body>
</html>
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function generate()
{
var myarray=[25, 8, 7, 41]
myarray.sort
(
function(a,b)
{
return b - a
}
)
alert ( myarray );
}
</script>
</head>
<body >
<form name=f1>
<input type=button value='Click for Sorting' onClick='generate()';>
</form>
</body>
</html>
7. Write a JavaScript to convert Celsius to Fahrenheit.
Shared By : Panchal Divyang s.(Asst Proff-D.L.Patel ) Show/Hide Program
<html>
<head>
<script language="javascript">
function convert(form,name)
{
if(name=="cel")
{
var f=5/9*(form.cel.value-32);
form.fer.value=f;
}
else
{
var c=9/5*(form.fer.value+32);
form.cel.value=c;
}
}
</script>
</head>
<body>
<form>
Celcious :<input type="text" name="cel" onblur="convert(this.form,this.name)">
<br/><br/>Ferenhit :<input type="text" name="fer" onblur="convert(this.form,this.name)">
</form>
</body>
</html>
<script type="text/javascript">
function convert(Totalvalue)
{
var Far=(Totalvalue-32) * 5 / 9;
document.write(Totalvalue + ' Celsius = ' + Far + ' Farenheit');
}
</script>
<body onload="convert(102)">
</body>
<!--GTU LIST & --=Write a JavaScript to convert Celsius to Fahrenheit.-->
<html>
<head>
<script language ="Javascript">
function convert()
{
if(t1.value!= "")//check if first value is not null
{
var tmp=parseFloat(t1.value)*33.8;//convert it to float
t2.value=tmp;//storing of above calculation into t2
}
else//if first value is null
{
t2.value="";
t1.focus();//only focus on 1st textbox
alert("Enter Value := ");
}
}//end of function
</script>
</head>
<body>
<center>
<h1>Convertion of Celsius to Fahrenheit</h1>
Celsius: <input type=text name=t1 onKeyup="convert()">
<br>
<br>
Fahrenheit :<input type=text name=t2 readonly>
</center>
</body>
</html>
8. Write a JavaScript to find a string from the given text...
<html>
<head>
<script type="text/javascript">
function findout(form)
{
var str=new String(form.string1.value);
form.string1.value=str.replace(form.find.value,form.replac.value);
}
</script>
</head>
<body >
<form>
Enter a Text : <input type="text" name="string1" size=50><br/><br/>
Word to find : <input type="text" name="find" ><br/>
Word to Replace : <input type="text" name="replac"><br/>
<input type="button" value="REPLACE" onClick="findout(this.form)">
</form>
</body>
</html>
<HTML>
<HEAD>
<TITLE> STRING </TITLE>
<SCRIPT LANGUAGE="javascript">
var i;
var j,f;
var myString;
var sString;
var strl,fl;
var temp=new Array();
function str_find()
{
myString=(document.getElementById("txt1").value);
sString=(document.getElementById("txt2").value);
strl=myString.length;
fl=sString.length;
for(i=0;i<strl;i++)
{
temp[i]=myString.substring(i,fl+i);
}
for(i=0;i<temp.length;i++)
{
if(temp[i]==sString)
{
f=0;
break;
}
else
f=1;
}
if(f==0)
alert("Found..");
else
alert("Not Found..");
}
</SCRIPT>
</HEAD>
<BODY>
<FONT FACE="Bookman Old Style"><H2>
4].Write a JavaScript to find a string from the given text.</H2></FONT><HR>
<BR><BR><BR>
<TABLE ALIGN="CENTER">
<TR>
<TD><B>Enter String : </B></TD>
<TD><INPUT TYPE="Text" ID="txt1" SIZE="30%" ALIGN="right"></TD>
</TR>
<TR>
<TD><B>Enter String To Search : </B></TD>
<TD><INPUT TYPE="Text" ID="txt2" SIZE="30%"></TD>
</TR>
<TR></TR><TR></TR><TR></TR><TR></TR><TR></TR><TR></TR><TR></TR>
<TR>
<TD COLSPAN="2" align="center"><INPUT TYPE="button" VALUE=" SEARCH" onClick="str_find()"><TD>
</TR>
</TABLE>
</BODY>
</HTML>
9. Write a JavaScript to show a pop up window with...
Shared By : Maitrey Mehta.(Kalol-MCA) Show/Hide Program
<html>
<head>
<style>
.rb { border : 10px solid black}
</style>
<script>
function check()
{
var opop=window.createPopup();
var a,b;
if(openwin.name.value=="")
{
alert("Enter Your Name");
openwin.name.focus();
}
else
{ a=window.open("", "mywindow", "location=1,status=1,
scrollbars=1,height=500,width=600 style=\"border : 10px \"");
a.document.write("Wel Come "+openwin.name.value.toUpperCase());
a.document.write('<br><input type="button" onClick="self.close()" value="CLOSE"></p>');
a.document.body.bgColor='lime';
a,document.body.border=10;
b=a.document.body;
b.style.borderWidth=25;
b.style.borderStyle='solid';
}
}
</script>
</head>
<body class="rb">
<form name="openwin">
Enter Name<input type="text" name="name" value="" style="color:red"><br>
<input type="button" name="open" value="Open New Window" style="color:green" onClick="check();">
</form>
</body>
</html>
<%@page contentType="text/html" import="java.util.*" %>
<html>
<head>
<title>JavaScript Popup Example</title>
</head>
<script type="text/javascript">
function poponload()
{
testwindow = window.open("", "mywindow", "location=1,status=1,scrollbars=1,width=300,height=100,border:2px solid #ffffff;");
testwindow.moveTo(200, 200);
testwindow.document.write('<h1>HELLO!</h1>');
testwindow.document.bgColor = "#BFFF00";
}
</script>
<body onload="poponload()">
<h1>JavaScript Popup Example 3</h1>
</body>
</html>
10. Write a Servlet to display “Hello World” on browser...
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Ex10Http extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("Hello World....");
}
}