CSS_Practical_Answers[1]
CSS_Practical_Answers[1]
1. Write Javascript code for arithmetic expression evaluation and print the result using
message box. (Accept the values from user)
<!doctype html>
<html>
<head>
</head>
<body>
<script>
var num1 =parseInt(prompt("Enter first number"));
var num2 =parseInt(prompt("Enter second number"));
var sum=num1+num2;
var mul=num1*num2;
document.write("Addition "+sum+ "</br>");
document.write("Multiplication "+mul);
</script>
</body>
</html>
OR by using getElementById methode
<html>
<head>
<script>
function add()
{
var a,b,c;
a=Number(document.getElementById("First").value)
b=Number(document.getElementById("Second").value)
c=a+b;
document.getElementById("result").value=c;
}
function Sub()
{
var a,b,c;
a=Number(document.getElementById("First").value)
b=Number(document.getElementById("Second").value)
c=a-b;
document.getElementById("result").value=c;
}
function Mul()
{
var a,b,c;
a=Number(document.getElementById("First").value)
b=Number(document.getElementById("Second").value)
c=a*b;
document.getElementById("result").value=c;
}
function Div()
{
var a,b,c;
a=Number(document.getElementById("First").value)
b=Number(document.getElementById("Second").value)
c=a/b;
document.getElementById("result").value=c;
}
</script>
</head>
<body>
Enter the First Number=<input id="First"><br><br><br>
Enter the Second Number=<input id="Second"><br><br><br>
2.1 Write Javascript code for finding the largest number from given three numbers.
(accept numbers from user).
<!doctype html>
<html>
<head>
</head>
<body>
<script>
var input1 = parseInt(prompt("Enter first number:"));
var input2 = parseInt(prompt("Enter second number:"));
var input3 = parseInt(prompt("Enter third number:"));
if ((input1 == input2) && (input1 == input3)) {
document.write("All numbers are equal");
} else if ((input1 > input2) && (input1 > input3)) {
document.write(input1 + " is larger than " + input2 + " and " + input3);
} else if ((input2 > input1) && (input2 > input3)) {
document.write(input2 + " is larger than " + input1 + " and " + input3);
} else if ((input3 > input1) && (input3 > input2)) {
document.write(input3 + " is larger than " + input2 + " and " + input1);
}
</script>
</body>
</html>
2.2 Write Javascript code to display the numbers from 1 to 10 using for loop.
<!doctype html>
<html>
<head>
</head>
<body>
<script>
for (var i = 1; i <= 10; i++)
{
document.write(i+"</br>");
}
</script>
</body>
</html>
2.3 Write Javascript code to find the factorial of given number. (use while loop)
<!doctype html>
<html>
<head>
</head>
<body>
<script>
var n=prompt("Enter one Letter");
var i=1,fact=1;
do{
fact=fact*i;
i++;
}
while(i<=n);
<html>
<head>
</head>
<body>
<script>
var stud=["Ram","Gopal","Ganesh","Shree","Shyam"];
var i;
for(i=0;i<stud.length;i++)
{
document.write(stud[i]+"<br>");
}
</script>
</body>
</html>
3.2 Write Javascript code to sort given elements of an array.
<!DOCTYPE html>
<html>
<body>
<script>
var arr = [30,10,20,50,40];
for(var i=0; i<arr.length; i++)
{
document.write(arr[i] +"</br>");
}
document.write("The array is sorted"+"</br>");
arr.sort();
for(var i=0; i<arr.length; i++)
{
document.write(arr[i] +"</br>");
}
</script>
</body>
</html>
4.1 Write Javascript function to calculate the area of circle.
<html>
<head>
<title>Find the area and circumference of a circle</title>
</head>
<body>
<script language="JavaScript">
function CalculateArea(){
var radius =document.form1.Radius.value;
document.write("<P>The area of the circle is " + (radius * radius * Math.PI) + "</p>");
}
</script>
<form name=form1>
Enter the radius of circle:
<input type="text" name="Radius" size=10>
<br>
<input type="button" value="Calculate" onClick='CalculateArea();'>
</form>
</body>
</html>
4.2 Write Javascript function to perform addition of two numbers. (accept the values from
user)
<html>
<head>
<title>Add program</title>
</head>
<script language="javascript">
function addNumbers()
{
var val1 =parseInt(document.form1.value1.value);
var val2 =parseInt(document.form1.value2.value);
var add= val1 + val2;
document.write("<P>Addition is " + add + "</p>");
}
</script>
<body>
<form name=form1>
value1 = <input type="text" id="value1" name="value1" value=""/>
value2 = <input type="text" id="value2" name="value2" value=""/>
<input type="button" name="Sumbit" value="Click here" onclick="addNumbers()"/>
</form>
</body>
</html>
5.1 Write Javascript code to create the webpage using different form elements. (Design the
student registration form)
<html>
<head>
<title> Registration Page </title>
</head>
<body bgcolor="Lightskyblue">
<br>
<br>
<form>
6.1 Write Javascript code to create the login form using different form events
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Form Event</title>
</head>
<body>
9.1. Write simple Javascript code to create window and open it in new blank page of browser.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open an about:blank page in a new browser window that is 200px wide and
100px tall.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "", "width=200,height=100");
}
</script>
</body>
</html>
9.2. Write Javascript code to create new window and set its position using different attributes.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to open a new window with some specifications.</p>
<button onclick="myFunction()">Open Window</button>
<script>
function myFunction() {
window.open("https://
www.google.com","_blank","toolbar=yes,scrollbars=yes,resizable=yes,top=0,left=0,width=400,heig
ht=400");
}
</script>
</body>
</html>
9.3. Write Javascript code to create new window with scroll attribute.
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 5000px;
}
</style>
</head>
<body>
<p>Click the button to scroll the document window 300 pixels horizontally and 500 pixels
vertically.</p>
<button onclick="scrollWin()">Click me to scroll</button>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<script>
function scrollWin() {
window.scrollTo(300, 500);
}
</script>
</body>
</html>
9.4. Write Javascript code to create new window with timing events.
1. setTimeout
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Well Come");
}
</script>
</head>
<body>
<input type="button" value="Open Window" onclick="setTimeout(myFunction,4000);">
</body>
</html>
2. The setInterval()
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Well Come");
}
</script>
</head>
<body>
<input type="button" value="Open Window" onclick="setInterval(myFunction,4000);">
</body>
</html>
10.1. Write Javascript code to create regular expression using global, case insensitive modifier.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = "Is this all there is?";
var patt1 = /is/g;
var patt2 = /All/i;
var result = str.match(patt1);
var result1 = str.match(patt2);
document.write(result+"<br/>");
document.write(result1);
}
</script>
</body>
</html>
10.2. Write Javascript code to check whether the string entered by user contains digit or not.
<!DOCTYPE html>
<html>
<head>
<script type= "text/javascript">
function myFunction(str) {
var re=/[^0-9]/;
if(re.test(str))
{
alert("String does not contain any digit");
}
else
{
alert("String contain any digit");
}
}
</script>
</head>
<body>
<script type= "text/javascript">
var input_str = prompt("Enter some input here");
myFunction(input_str);
</script>
</body>
</html>
<html>
<head>
<script language="javascript" type="text/javascript">
function dynamicdropdown(listindex)
{
switch (listindex)
{
case "boys" :
document.getElementById("status").options[0]=new Option("Students","");
document.getElementById("status").options[1]=new Option("Sunil");
document.getElementById("status").options[2]=new Option("Aditya");
document.getElementById("status").options[3]=new Option("Siddharth");
break;
case "girls" :
document.getElementById("status").options[0]=new Option("Students","");
document.getElementById("status").options[1]=new Option("Archana");
document.getElementById("status").options[2]=new Option("Sharda");
document.getElementById("status").options[3]=new Option("Swati");
break;
}
return true;
}
</script>
</head>
<title>Dynamic Drop Down List</title>
<body>
<h2>Dynamically Changing Menu</h2>
<select id="source" name="source" onchange="javascript:
dynamicdropdown(this.options[this.selectedIndex].value);">
<option value="">Class</option>
<option value="boys">Boys</option>
<option value="girls">Girls</option>
</select>
<script type="text/javascript" language="JavaScript">
document.write('<select name="status" id="status"><option value="">Students</option></select>')
</script>
</body>
</html>
12.1. Write Javascript code to create advertisement banner with different images.
(1) We have created four images and we named them as bannerl.jpg, banner2.jpg, banner3.jpg, and
banner4.jpg.
(2) Save the images in the same folder as the HTML file which will be used to show the banners.
These images can be created by using some graphics tools or image files can be downloaded from
Internet.
Java Script
<!DOCTYPE html>
<html>
<head>
<script language="Javascript">
MyBanners =new Array('banner1.jpg','banner2.jpg', 'banner3.jpg');
banner_count=0;
function DisplayBanners()
{
if (document.images)
{
banner_count++;
if (banner_count==MyBanners.length)
{
banner_count=0;
}
document.BannerChange.src=MyBanners[banner_count];
setTimeout("DisplayBanners()",2000);
}
}
</script>
</head>
<body onload="DisplayBanners()">
<center> <h1> Displaying Banner </h1>
<img src="bannerl.jpg" width="900" height="120" name="BannerChange">
</center>
</body>
</html>
<html>
<head>
<script>
Silder=new
Array('download1.jpg','download2.jpg','download3.jpg','download4.jpg','download5.jpg');
i=0;
function display(sn)
{
i=i+sn;
if(i>Silder.length-1)
{
i=0;
}
if(i<0)
{
i=Silder.length-1;
}
document.SlideID.src=Silder[i];
}
</script>
</head>
<body>
<center>
<h1>Nature Photo</h1>
<img src="nature.jpg" name="SlideID" width="800" height="400"><br><br>
<button onclick="display(-1)">Back</button>
<button onclick="display(1)">Next</button></center>
</body>
</html>
8.1. Write Javascript code to delete the cookie after specific period.
Example 1
In this example, we use expire attribute to delete a cookie by providing expiry date (i.e. any past
date) to it.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="name=Martin ; expires=Sun, 20 Aug 2000 12:00:00 UTC";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not avaliable");
}
}
</script>
</body>
</html>
Example 2
In this example, we use max-age attribute to delete a cookie by providing zero or negative number
(that represents seconds) to it.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="name=Martin ;max-age=0";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not avaliable");
}
}
</script>
</body>
</html>
Example 3
Let's see an example to delete a cookie explicitly.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="name=Martin ";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not avaliable");
}
}
</script>
</body>
</html>