JavaScript IMP Questions by Vishal Sir
JavaScript IMP Questions by Vishal Sir
Author:
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
CSS/JavaScript Important Questions
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
CSS/JavaScript Important Questions
<html>
<body>
<script language="javascript" type="text/javascript">
var no=prompt("Enter Number:");
var flag=0;
for(i=2;i<no;i++)
{
if(no%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
document.write("Number is Prime");
}
else
{
document.write("Number is not Prime");
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
CSS/JavaScript Important Questions
Example:
<html>
<body>
<form name="myform">
<input type="text" name="tf1" id="id1">
<input type="button" name="b1" value="OK" onmouseup="display1()" onblur="display2()">
</form>
<script language="javascript" type="text/javascript">
function display1()
{
document.getElementById("id1").value="onmouseup event occurred";
}
function display2()
{
document.getElementById('id1').value="onblur event occurred";
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
CSS/JavaScript Important Questions
<html>
<body>
<input type="button" name="b1" value="Website1" onclick="OpenWindow('https://www.vjtechacademy.in')">
<input type="button" name="b2" value="Website2" onclick="OpenWindow('https://msbte.org.in/')">
Applications:
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
CSS/JavaScript Important Questions
8. Write a Java script to create person object with properties firstname, lastname, age, eyecolor,
delete eyecolor property and display remaining properties of person object.
<html>
<body>
<script>
var person = {
firstname:"Vishal",
lastname:"Jadhav",
age:29,
eyecolor:"blue"
};
delete person.eyecolor;
document.write("After delete properties of person object: ")
document.write("<br>First Name:"+person.firstname);
document.write("<br>Last Name:"+person.lastname);
document.write("<br>age:"+person.age);
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
CSS/JavaScript Important Questions
9. Write a Java script that initializes an array called flowers with the names of 3 flowers. The
script then displays array elements.
<html>
<body>
<script>
var flowers = new Array();
flowers[0] = 'Rose';
flowers[1] = 'Mogra';
flowers[2] = 'Lotus';
for (var i = 0; i < flowers.length; i++)
{
document.write(flowers[i] + '<br>');
}
</script>
</body>
</html>
<html>
<body>
<input type="button" name="b1" value="Click Me" onclick="display()">
<script>
function display()
{
document.write("This is user defined function");
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
CSS/JavaScript Important Questions
11. Write a Javascript to design a form to accept values for user ID & password.
<html>
<body>
<form name="myform">
Enter User ID:<input type="text" id="tf1">
Enter Password:<input type="password" id="tf2">
<input type="button" name="b1" value="Display" onclick="show()">
</form>
<script>
function show()
{
var id=document.getElementById("tf1").value;
var psw=document.getElementById("tf2").value;
document.write("<br>Your User ID:"+id);
document.write("<br>Your Password:"+psw);
}
</script>
</body>
</html>
1. assign( )
2. reload( )
3. replace( )
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
CSS/JavaScript Important Questions
13. Explain getter and setter properties in Java script with suitable example
In JavaScript, there are two kinds of object properties:
Data properties
Accessor properties
Accessor Property
In JavaScript, accessor properties are methods that get or set the value of an object. For that,
we use these two keywords:
JavaScript Getter
In JavaScript, getter methods are used to access the properties of an object. For example,
<html>
<body>
<script language="javascript" type="text/javascript">
var student={
FirstName:'Brenden',
get getData()
{
return this.FirstName;
}
};
document.write(student.FirstName);
document.write(student.getData);
</script>
</body>
</html>
In the above program, a getter method getName() is created to access the property of an
object.
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
CSS/JavaScript Important Questions
JavaScript Setter
<html>
<body>
<script language="javascript" type="text/javascript">
var student={
FirstName:'Brenden',
set putData(fn)
{
this.FirstName=fn;
}
};
document.write(student.FirstName);
student.putData='Vishal';
document.write(student.FirstName);
</script>
</body>
</html>
In the above example, the setter method is used to change the value of an object.
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
CSS/JavaScript Important Questions
14. Explain prompt() and confirm() method of Java script with syntax and example.
prompt()
The prompt () method displays a dialog box that prompts the visitor for input.
The prompt () method returns the input value if the user clicks "OK".
If the user clicks "cancel" the method returns null.
Syntax: window.prompt (text, defaultText)
Example:
<html>
<body>
<script type="text/javascript">
function msg()
{
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
<input type="button" value="click" onclick="msg()"/>
</body>
</html>
confirm()
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
CSS/JavaScript Important Questions
15. Write a Java script program which computes, the average marks of the following students
then, this average is used to determine the corresponding grade.
<html>
<body>
<script language="javascript" type="text/javascript">
var students = [['Summit', 80], ['Kalpesh', 77], ['Amit', 88], ['Tejas',
93],['Abhishek', 65]];
var total = 0;
for (var i=0; i < students.length; i++)
{
total += students[i][1];
}
var avg = (total/students.length);
document.write("Average Marks: " + avg);
document.write("<br>");
if (avg < 60)
{
document.write("Grade : E");
}
else if (avg < 70)
{
document.write("Grade : D");
}
else if (avg < 80)
{
document.write("Grade : C");
}
else if (avg < 90)
{
document.write("Grade : B");
}
else if (avg < 100)
{
document.write("Grade : A");
}
</script>
</body>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
CSS/JavaScript Important Questions
</html>
16. Write the use of chatAt() and indexof() with syntax and example.
charAt()
The charAt() method requires one argument i.e is the index of the character that you
want to copy.
Syntax:
SingleCharacter = NameOfStringObject.charAt(index);
Example: var FirstName = 'Vishal';
var Character = FirstName.charAt(0); //V
indexOf()
The indexOf() method returns the index of the character passed to it as an argument.
If the character is not in the string, this method returns –1.
Syntax:
var indexValue = string.indexOf('character');
Example:
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
CSS/JavaScript Important Questions
18. Write a JavaScript that will replace following specified value with another value in string.
String = “I will fail”
Replace “fail” by “pass”
<html>
<body>
<script>
var myStr = "I will fail";
var newStr = myStr.replace("fail", "pass");
document.write(newStr);
</script>
</body>
</html>
19. Write a Java Script code to display 5 elements of array in sorted order.
<html>
<body>
<script>
var arr1 = [ “Red”, “red”, “Blue”, “Green”]
document.write(“Before sorting arra1=” + arr1);
document.write(“<br>After sorting arra1=” + arr1.sort());
</script>
</body>
</html>
20. Explain open() method of window object with syntax and example.
The open() method of window object is used to open a new window and loads the
document specified by a given URL.
MyWindow = window.open()
The open() method returns a reference to the new window, which is assigned to the
MyWindow variable.
You then use this reference any time that you want to do something with the window
while your JavaScript runs.
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
CSS/JavaScript Important Questions
A window has many properties, such as its width, height, content, and name—to
mention a few.
You set these attributes when you create the window by passing them as parameters to
the open() method:
The first parameter is the full or relative URL of the web page that will appear in the new
window.
The second parameter is the name that you assign to the window.
The third parameter is a string that contains the style of the window.
We want to open a new window that has a height and a width of 250 pixels and displays
an advertisement that is an image. All other styles are turned off.
Syntax: MyWindow = window.open(‘webpage1.html’, 'myAdWin', 'status=0, toolbar=0,
location=0, menubar=0, directories=0, resizable=0, height=250, width=250')
<html>
<head>
<script language="javascript" type="text/javascript">
function DisplayNewWindow()
{
var x1=window.open("https://www.msbte.org.in","w1","top=200,left=100,width=500,height=500");
}
</script>
</head>
<body>
<form name="myform">
<input type="button" name="b1" value="Show New Window" onclick="DisplayNewWindow()">
</form>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
CSS/JavaScript Important Questions
21. Describe regular expression. Explain search () method used in regular expression with suitable
example.
Regular Expression:
A regular expression is very similar to a mathematical expression, except a regular
expression tells the browser how to manipulate text rather than numbers by using
special symbols as operators.
Search() method:
str.search() method takes a regular expression/pattern as argument and search for the
specified regular expression in the string.
This method returns the index where the match found.
Example:
<html>
<body>
<script>
function myFunction()
{
var str = "Good Morning!";
var regEx=/Morning/i;
var n = str.search(regEx);
document.write(n);
}
myFunction();
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
CSS/JavaScript Important Questions
22. List ways of protecting your web page and describe any one of them.
Ways of protecting Web Page:
1) Hiding your source code
2) Disabling the right Mouse Button
3) Hiding JavaScript
4) Concealing E-mail address.
- Also, we know that by using mouse button we can open the web page source code including
javascript code.
- So, we need to take proper action on this point so that hacker cannot see our code.
- Viewing source code by user is not safe hence disabling right mouse button is important.
<html>
<head>
<script language="javascript" type="text/javascript">
function display()
{
document.addEventListener('contextmenu',event=>event.preventDefault());
}
</script>
</head>
<body>
<input type="button" name="b1" value="Protect Web Page" onclick="display()">
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
CSS/JavaScript Important Questions
23. Create a slideshow with the group of three images, also simulate next and previous transition
between slides in your Java Script.
<html>
<body>
<img src="Apple.png" width="500" height="300" id="image1"> <br><br><br><br>
<input type="button" value="Previous" onclick="SlideShow(-1)">
<input type="button" value="Next" onclick="SlideShow(1)">
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
CSS/JavaScript Important Questions
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
CSS/JavaScript Important Questions
25. Write a Java script to modify the status bar using on MouseOver and on MouseOut with links.
When the user moves his mouse over the links, it will display “MSBTE” in the status bar. When
the user moves his mouse away from the link the status bar will display nothing.
<html>
<head>
<title>JavaScript Status Bar</title>
</head>
<body>
<a href="https://msbte.org.in/" onMouseOver="window.status='MSBTE'"
onMouseOut="window.status="> MSBTE </a>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
CSS/JavaScript Important Questions
26. Write a HTML script which displays 2 radio buttons to the users for fruits and vegetables and 1
option list. When user select fruits radio button option list should present only fruits names to
the user & when user select vegetable radio button option list should present only vegetable
names to the user.
<html>
<body>
<form name="myform" action="" method="post">
<select name="optionList" size="2">
<option value=1>Mango
<option value=2>Banana
<option value=3>Apple
</select>
<br>
<input type="radio" name="grp1" value=1 checked="true"
onclick="updateList(this.value)">Fruits
<input type="radio" name="grp1" value=2
onclick="updateList(this.value)">Vegetables
<br>
</form>
<script language="javascript" type="text/javascript">
function updateList(ElementValue)
{
with(document.forms.myform)
{
if(ElementValue == 1)
{
optionList[0].text="Mango";
optionList[0].value=1;
optionList[1].text="Banana";
optionList[1].value=2;
optionList[2].text="Apple";
optionList[2].value=3;
}
if(ElementValue == 2)
{
optionList[0].text="Potato";
optionList[0].value=1;
optionList[1].text="Cabbage";
optionList[1].value=2;
optionList[2].text="Onion";
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
CSS/JavaScript Important Questions
optionList[2].value=3;
}
}
}
</script>
</body>
</html>
27. Describe, how to read cookie value and write a cookie value. Explain with example.
Web Browsers and Servers use HTTP protocol to communicate and HTTP is a stateless
protocol.
But for a commercial website, it is required to maintain session information among
different pages. For example, one user registration ends after completing many pages.
But how to maintain users' session information across all the web pages.
Cookies are a plain text data record of 5 variable-length fields
o Expires − The date the cookie will expire. If this is blank, the cookie will expire when
the visitor quits the browser.
o Domain − The domain name of your site.
o Path − The path to the directory or web page that set the cookie. This may be blank
if you want to retrieve the cookie from any directory or page.
o Secure − If this field contains the word "secure", then the cookie may only be
retrieved with a secure server. If this field is blank, no such restriction exists.
o Name=Value − Cookies are set and retrieved in the form of key-value pairs
Cookies were originally designed for CGI programming. The data contained in a cookie is
automatically transmitted between the web browser and the web server, so CGI scripts on
the server can read and write cookie values that are stored on the client.
JavaScript can also manipulate cookies using the cookie property of the Document object.
JavaScript can read, create, modify, and delete the cookies that apply to the current web
page.
Example:
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
CSS/JavaScript Important Questions
<html>
<head>
<script language="javascript" type="text/javascript">
function WriteCookie()
{
var nm=document.getElementById("id1").value;
document.cookie="name="+nm+";expires=Sun, 31 Jan 2023 00:00:00 GMT";
alert("Cookie Written Successfully");
}
function ShowCookie()
{
var x=document.cookie;
alert(x);
}
</script>
</head>
<body>
<form name="myform">
Enter Your Name:<input type="text" name="tf1" id="id1">
<input type="button" name="b1" value="Create Cookie" onclick="WriteCookie()">
<input type="button" name="b2" value="Get Cookie" onclick="ShowCookie()">
</form>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
CSS/JavaScript Important Questions
28. Describe how to evaluate checkbox selection. Explain with suitable example.
Evaluating Checkbox Selection:
A checkbox is created by using the input element with the type=”checkbox” attribute-value
pair.
A checkbox in a form has only two states(checked or un-checked) and is independent of the
state of other checkboxes in the form. Check boxes can be grouped together under a
common name.
You can write javascript function that evaluates whether or not a check box was selected
and then processes the result according to the needs of your application.
Example:
<html>
<body>
<form name="myform">
Select your favourite Programming Language:<br>
<input type="checkbox" name="c1" id="id1" value="c lang">C Lang
<input type="checkbox" name="c2" id="id2" value="c++ lang">C++ Lang
<input type="checkbox" name="c3" id="id3" value="java lang">Java Lang
<input type="checkbox" name="c4" id="id4" value="python lang">Python Lang
<input type="button" name="b1" value="Show" onclick="EvaluateChecbox()">
</form>
<script language="javascript" type="text/javascript">
function EvaluateChecbox()
{
var m="You have selected: ";
if(document.getElementById("id1").checked==true)
{
m=m+document.getElementById("id1").value+" ";
}
if(document.getElementById("id2").checked==true)
{
m=m+document.getElementById("id2").value+" ";
}
if(document.getElementById("id3").checked==true)
{
m=m+document.getElementById("id3").value+" ";
}
if(document.getElementById("id4").checked==true)
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
CSS/JavaScript Important Questions
{
m=m+document.getElementById("id4").value+" ";
}
alert(m);
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
CSS/JavaScript Important Questions
29. Write a script for creating following frame structure. FRUITS, FLOWERS AND CITIES are links to
the webpage fruits.html, flowers.html, cities.html respectively. When these links are clicked
corresponding data appears in FRAME 3.
<html>
<body>
<table border="1">
<tr>
<td align="center" colspan="2">
FRAME 1
</td>
</tr>
<tr>
<td>
FRAME 2
<ul>
<li>
<a href="fruits.html" target="mainframe">FRUITS</a>
</li>
<li>
<a href="flowers.html" target="mainframe">FLOWERS</a>
</li>
<li>
<a href="cities.html" target="mainframe">CITIES</a>
</li>
</ul>
</td>
<td>
FRAME 3<BR>
<iframe name="mainframe"></iframe>
</td>
</tr>
</table>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
CSS/JavaScript Important Questions
30. Write a javascript to create a pull-down menu with three options [Google, MSBTE, Yahoo]
once the user will select one of the options then user will be redirected to that site.
<html>
<body>
<form name="myform" action="" method="post">
Select Your Favourite Website:
<select name="MenuChoice" onchange="getPage(this)">
<option
value="https://www.google.com">Google</option>
<option
value="https://www.msbte.org.in">MSBTE</option>
<option
value="https://www.yahoo.com">Yahoo</option>
</select>
</form>
<script language="javascript" type="text/javascript">
function getPage(choice)
{
page=choice.options[choice.selectedIndex].value;
if(page!= "")
{
window.location=page;
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
CSS/JavaScript Important Questions
31. Write the syntax of and explain use of following methods of JavaScript Timing Event.
a. setTimeout()
b. setInterval()
- Window objects provide predefined timer functions which allow you to register function to be
invoked once or any no of times depending upon specified time.
- In JavaScript, we can write some code that will execute at specified time intervals. This is called
timing events.
1) var id=setTimeout(function,delay) - It will initiates a single timer which will call the specified
function after the delay. This function return unique id with which the timer can be canceled at
a later time.
2) var id=setInterval(function,delay) - Similar to setTimeout method but it will call the function
continually until it canceled. This method will help us to execute function which you want to call
again and again.
Program:
-------------
<html>
<head>
<script language="javascript" type="text/javascript">
var number=0;
var timerID=null;
function WriteNos()
{
if(timerID==null)
{
timerID=setInterval("display()",500);
}
}
function display()
{
if(number<=15)
{
document.getElementById("id1").innerHTML+=number+" ";
number=number+1;
}
else
{
clearInterval(timerID);
}
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
CSS/JavaScript Important Questions
}
</script>
</head>
<body>
<input type="button" name="b1" value="Click Me" onclick="WriteNos()">
<textarea name="ta1" id="id1" rows=5 cols=20></textarea>
</body>
</html>
32. Write a java script that displays textboxes for accepting name & email ID & a submit button.
Write java script code such that when the user clicks on submit button (1) Name Validation (2)
Email ID Validation.
<html>
<body>
<form name="myform">
Enter Your Name:<input type="text" name="tf1">
Enter Email ID:<input type="text" name="tf2">
<input type="button" Value="Validation" onclick="ValidateForm()">
</form>
<script>
function ValidateForm()
{
if(document.myform.tf1.value=="")
{
alert( "Please provide your name!" );
}
else if(document.myform.tf2.value=="")
{
alert("Please provide your Email!" );
}
else
{
var emailID =document.myform.tf2.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
}
else
{
alert("Validation done successfully");
}
}
}
</script>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
CSS/JavaScript Important Questions
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
CSS/JavaScript Important Questions
34. Design a webpage that displays a form that contains an input for user name and password.
User is prompted to enter the input user name and password and password become value of
the cookies. Write the javascript function for storing the cookies.
<html>
<body>
<form name="myform">
Enter User Name:<input type="text" name="tf1" id="id1">
Enter Password:<input type="text" name="tf2" id="id2">
<input type="button" name="b1" value="Create Cookie" onclick="WriteCookie()">
</form>
<script language="javascript" type="text/javascript">
function WriteCookie()
{
var nm=document.getElementById("id1").value;
var val=document.getElementById("id2").value;
document.cookie=nm+"="+val+";expires=Tues,31 Jan 2024 00:00:00 GMT";
alert("Cookie Written Successfully");
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 31
CSS/JavaScript Important Questions
35. Write a javascript program to create read, update and delete cookies.
<html>
<body>
<form name="myform">
Enter Name of Cookie:<input type="text" name="tf1" id="id1">
Enter Value of Cookie:<input type="text" name="tf2" id="id2">
<input type="button" name="b1" value="Create Cookie" onclick="WriteCookie()">
<input type="button" name="b2" value="Read Cookie" onclick="ReadCookie()">
<input type="button" name="b3" value="Update Cookie" onclick="UpdateCookie()">
<input type="button" name="b4" value="Delete Cookie" onclick="DeleteCookie()">
</form>
<script language="javascript" type="text/javascript">
function WriteCookie()
{
var nm=document.getElementById("id1").value;
var val=document.getElementById("id2").value;
document.cookie=nm+"="+val+";expires=Tues,31 Jan 2024 00:00:00 GMT";
alert("Cookie Written Successfully");
}
function ReadCookie()
{
var x=document.cookie;
alert(x);
}
function UpdateCookie()
{
var nm=document.getElementById("id1").value;
var val=document.getElementById("id2").value;
document.cookie=nm+"="+val+";expires=Tues,31 Jan 2024 00:00:00 GMT";
alert("Cookie updated Successfully");
}
function DeleteCookie()
{
var nm=document.getElementById("id1").value;
var val=document.getElementById("id2").value;
document.cookie=nm+"="+val+";expires=Tues,31 Jan 1999 00:00:00 GMT";
alert("Cookie deleted Successfully");
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 32
CSS/JavaScript Important Questions
36. Q.1 Write a javascript program to link banner advertisements to different URLs.
Q.2 Develop a JavaScript Program to Create Rotating Banner Ads with URL Links
<html>
<body onload="ShowBanners()">
<a href="javascript:ShowLinks()">
<img src="clang.jpg" width="500" height="300" name="ChangeBanner"/>
</a>
<script language="Javascript">
MyBanners=new Array('clang.png','cpp.png','java.png')
MyBannerLinks=new
Array('http://msbte.org.in','http://www.google.com','http://www.gmail.com')
banner=0
function ShowLinks()
{
document.location.href=MyBannerLinks[banner]
}
function ShowBanners()
{
if(document.images)
{
banner++
if (banner==MyBanners.length)
{
banner=0
}
document.ChangeBanner.src=MyBanners[banner]
setTimeout("ShowBanners()",5000)
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 33
CSS/JavaScript Important Questions
37. Write a javascript program to calculate add, sub, multiplication and division of two number
(input from user). Form should contain two text boxes to input numbers of four buttons for
addition, subtraction, multiplication and division.
<html>
<body>
<form name="myform">
Enter 1st No:<input type="text" id="tf1">
Enter 2nd No:<input type="text" id="tf2">
Result:<input type="text" id="tf3">
<input type="button" name="b1" value="ADD" onclick="Addition()">
<input type="button" name="b2" value="SUB" onclick="Subtraction()">
<input type="button" name="b3" value="DIV" onclick="Division()">
<input type="button" name="b4" value="MUL" onclick="Multiplication()">
</form>
<script language="javascript">
function Addition()
{
var a=parseInt(document.getElementById("tf1").value);
var b=parseInt(document.getElementById("tf2").value);
var c=a+b;
document.getElementById("tf3").value=c;
}
function Subtraction()
{
var a=parseInt(document.getElementById("tf1").value);
var b=parseInt(document.getElementById("tf2").value);
var c=a-b;
document.getElementById("tf3").value=c;
}
function Division()
{
var a=parseInt(document.getElementById("tf1").value);
var b=parseInt(document.getElementById("tf2").value);
var c=a/b;
document.getElementById("tf3").value=c;
}
function Multiplication()
{
var a=parseInt(document.getElementById("tf1").value);
var b=parseInt(document.getElementById("tf2").value);
var c=a*b;
document.getElementById("tf3").value=c;
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 34
CSS/JavaScript Important Questions
38. Develop javascript to convert the given character to unicode and vice-versa
<html>
<body>
<script language="javascript" type="text/javascript">
var str="MSBTE";
document.write("ASCII Value of B="+str.charCodeAt(2)); //66
document.write("<br>Character Value of ASCII 65="+String.fromCharCode(65)); //A
</script>
</body>
</html>
39. Write a javascript function to generate Fibonacci series till user defined limit.
<html>
<body>
<script language="javascript" type="text/javascript">
function DisplayFibonancci()
{
var fno=0, sno=1, tno;
var no=parseInt(prompt("Enter Value of no"));
document.write("Fibonancci Series:"+fno+"\t"+sno);
for(i=2;i<no;i++)
{
tno=fno+sno;
document.write("\t"+tno);
fno=sno;
sno=tno;
}
}
DisplayFibonancci();
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 35
CSS/JavaScript Important Questions
40. State what is regular expression. Explain its meaning with the help of a suitable example.
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform pattern-matching and "search-and-replace"
functions on text.
Syntax
/pattern/modifiers;
Example:
Description
Modifier
Program:
<html>
<body>
<script language="javascript" type="text/javascript">
function CheckOperation()
{
var regex=/vjtech/gmi;
var str=document.getElementById("tf1").value;
var result=regex.test(str);
alert("Regular Expression Result:"+result);
}
</script>
Enter Text:<input type="text" id="tf1">
<input type="button" name="b1" value="Check" onclick="CheckOperation()">
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 36
CSS/JavaScript Important Questions
41. Write a JavaScript function that accepts a string as a parameter and find the length of the
string.
<html>
<body>
Enter Any Text:<input type="text" id="tf1">
<input type="button" name="b1" value="Find Length" onclick="CalcLength()">
<script language="javascript" type="text/javascript">
function CalcLength()
{
var str=document.getElementById("tf1").value;
var i=0;
var len=0;
while(str[i]! =null)
{
len++;
i++;
}
document.write("<br>Length of String="+len);
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 37
CSS/JavaScript Important Questions
42. Write a javascript program to validate email ID of the user using regular expression.
<html>
<body>
Enter Text:<input type="text" id="tf1">
<input type="button" name="b1" value="Validate" onclick="validateEmail()">
<script language="javascript" type="text/javascript">
function validateEmail()
{
var regex= /^ [a-zA-Z0-9_.] +\@[a-z] +\. +[a-z] {2,3} $/
var str=document.getElementById("tf1").value;
var result=regex.test(str);
if(result)
{
alert("Valid email id");
}
else
{
alert("Invalid email id");
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 38
CSS/JavaScript Important Questions
44. Write a javascript program to create a silde show with the group of six images, also simulate
the next and previous transition between slides in your javascript.
<html>
<head>
</head>
<body>
<img src="Apple.png" width="500" height="300" id="image1">
<input type="button" value="Previous" onclick="SlideShow(-1)">
<input type="button" value="Next" onclick="SlideShow(1)">
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 39
CSS/JavaScript Important Questions
- Example:
<html>
<body>
<script type = "text/javascript">
var a = true;
var b = false;
document.write("(a && b) => ");
result = (a && b);
document.write(result);
document.write("<br>");
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 40
CSS/JavaScript Important Questions
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 41
CSS/JavaScript Important Questions
47. Write a JavaScript that displays all properties of window object. Explain the code.
Program:
<html>
<body>
<script type="text/javascript">
for (var X in window)
{
document.write("<br>"+X);
}
</script>
</body>
</html>
Explanation of code:
- In above code, we have tried to display all properties of window object.
- Window is a predefined object of browser.
- In this code, we have tried to use for in loop which will iterate over the all properties of
window object.
- Window object property will assign to variable ‘X’ one bye one and inside the body of loop it
will display on browser.
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 42
CSS/JavaScript Important Questions
48. Write a JavaScript function to count the number of vowels in a given string
<html>
<body>
<script language="javascript" type="text/javascript">
var vowels = ["a", "e", "i", "o", "u"]
function countVowel(str)
{
var count=0;
str=str.toLowerCase();
for (letter of str)
{
if (vowels.includes(letter))
{
count++;
}
}
alert("No of Vowels:"+count);
}
var x = prompt("Enter a string:");
countVowel(x);
</script>
</body>
</html>
49. Write a function that prompts the user for a color and uses what they select to set the
background color of the new webpage opened.
<html>
<body>
document.body.style.background = ColorName;
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 43
CSS/JavaScript Important Questions
50. Write HTML Script that displays textboxes for accepting Name, middlename, Surname of the
user and a Submit button. Write proper JavaScript such that when the user clicks on submit
button
i) all texboxes must get disabled and change the color to “RED”. and with respective
labels. 3
ii) ii) Constructs the mailID as [email protected] and displays mail ID as message. (Ex. If
user enters Rajni as name and Pathak as surname mailID will be constructed as
[email protected]) .
<html>
<body>
Enter First Name:<input type="text" id="tf1"><br>
Enter Middle Name:<input type="text" id="tf2"><br>
Enter Surname:<input type="text" id="tf3"><br>
<input type="button" name="b1" value="Submit" onclick="display()">
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 44
CSS/JavaScript Important Questions
51. Write a JavaScript that displays first 20 even numbers on the document window.
<html>
<body>
<script language="javascript" type="text/javascript">
document.write("First 20 even numbers:");
for(i=1;i<=40;i++)
{
if(i%2==0)
{
document.write("<br>"+i);
}
}
</script>
</body>
</html>
52. Write a program to print sum of even numbers between 1 to 100 using for loop.
<html>
<body>
<script language="javascript" type="text/javascript">
var sum=0;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
document.write("Sum of EVEN numbers between 1 to 100 :"+sum);
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 45
CSS/JavaScript Important Questions
53. Write a JavaScript function to insert a string within a string at a particular position
<html>
<body>
alert(output);
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 46
CSS/JavaScript Important Questions
Webpage1.html
<html>
<body>
<center><h1>Frame1</h1></center>
</body>
</html>
Webpage2.html
<html>
<body>
<center><h1>Frame2</h1></center>
</body>
</html>
Webpage3.html
<html>
<body>
<center><h1>Frame3</h1></center>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 47
CSS/JavaScript Important Questions
MainWebPage.html
<html>
<head>
<frameset rows="25%,50%,25%">
<frame src="webpage1.html" />
<frame src="webpage2.html" />
<frame src="webpage3.html" />
</frameset>
</head>
</html>
55. Write a JavaScript program that create a scrolling text on the status line of a window.
<html>
<body>
<input type="button" name="b1" value="Status Bar" onclick="scrollText('Welcome to
status bar')">
<script language="javascript" type="text/javascript">
var scrollpos=0;
var maxscroll=100;
var blanks="";
function scrollText(text)
{
window.setInterval(displayText(text),500);
}
function displayText(text)
{
window.status=blanks+text;
++scrollpos;
blanks+=" ";
if(scrollpos>maxscroll)
{
scrollpos=0;
blanks="";
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 48
CSS/JavaScript Important Questions
56. Write a JavaScript that find and displays number of duplicate values in an array.
<html>
<body>
<script>
var array = [6, 9, 15, 6, 13, 9, 11, 15,100,100];
var index = 0, newArr = [];
var length = array.length;
function findDuplicates(arr)
{
for(var i=0;i<length-1;i++)
{
for(var j=i+1;j<length;j++)
{
if(arr[i]===arr[j])
{
newArr[index]=arr[i];
index++;
break;
}
}
}
document.write("Duplicates Value of Array:"+newArr);
}
findDuplicates(array);
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 49
CSS/JavaScript Important Questions
57. Construct regular expression for validating the phone number in following format only : (nnn)-
nnnn-nnnn OR nnn.nnnn.nnnn
<html>
<body>
Enter Mobile No:<input type="text" id="id1">
<input type="button" value="Validate" onclick="validation()">
<script>
function validation()
{
var regexp=/\([0-9]{3}\)\-[0-9]{3}\-[0-9]{3}\-[0-9]{4}/;
var x=document.getElementById("id1").value;
if(regexp.test(x))
{
window.alert("Valid Mobile no.");
}
else
{
window.alert("Invalid Mobile no.");
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 50
CSS/JavaScript Important Questions
58. Write HTML Script that displays dropdown list containing options NewDelhi, Mumbai,
Bangalore. Write proper JavaScript such that when the user selects any options corresponding
description of about 20 words and image of the city appear in table which appears below on
the same page.
<html>
<body>
<form name="myform">
Select Your Favourite City:
<select name="MenuChoice" onchange="getImage(this.value)">
<option value="city1.png">NewDelhi</option>
<option value="city2.png">Mumbai</option>
<option value="city3.png">Bangalore</option>
</select><br><br>
<table border="1">
<tr>
<td>
<img src="" id="id1" width="250" height="250"><br><br>
</td>
</tr>
<tr>
<td>
<p id="id2"></p>
</td>
</tr>
</table>
</form>
<script language="javascript" type="text/javascript">
function getImage(img)
{
if(img=="city1.png")
{
document.getElementById("id1").src=img;
document.getElementById("id2").innerHTML="This is capital city of India";
}
else if(img=="city2.png")
{
document.getElementById("id1").src=img;
document.getElementById("id2").innerHTML="This is capital city of
Maharashtra";
}
else if(img=="city3.png")
{
document.getElementById("id1").src=img;
document.getElementById("id2").innerHTML="This is capital city of Karnataka";
}
}
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 51
CSS/JavaScript Important Questions
</script>
</body>
</html>
59. Write a webpage that accepts Username and adharcard as input texts. When the user enters
adhaarcard number, the JavaScript validates card number and diplays whether card number is
valid or not. (Assume valid adhaar card format to be nnnn.nnnn.nnnn or nnnn-nnnn-nnnn).
<html>
<body>
Enter Aadhar No:<input type="text" id="id1">
<input type="button" value="Validate" onclick="validation()">
<script>
function validation()
{
var regexp= /^[0-9]{4}[.-]?[0-9]{4}[ .-]?[0-9]{4}$/;
var x=document.getElementById("id1").value;
if(regexp.test(x))
{
window.alert("Valid Aadhar no.");
}
else
{
window.alert("Invalid Aadhar no.");
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 52
CSS/JavaScript Important Questions
60. Write a javascript to create option list containing list of images and then display images in
new window as per selection.
<html>
<body>
<form name="myform">
Select Your Favourite Image:
<select name="MenuChoice" onchange="getImage(this.value)">
<option value="city1.png">Mumbai</option>
<option value="city2.png">Pune</option>
<option value="city3.png">Thane</option>
</select>
</form>
<script language="javascript" type="text/javascript">
function getImage(img)
{
window.open(img,"vjtech","width=500,height=500 top=100 left=100");
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 53
CSS/JavaScript Important Questions
62. State the use of dot syntax in JavaScript with the help of suitable example.
In JavaScript, you refer to the methods and properties of an object using the Dot Syntax.
When you do this, you must include the name of the object. JavaScript uses the Dot Syntax
to separate the name of the object from its properties and methods.
One of the best example is: - document. write("Hello World! ");
Here you are using the method write () of the document object
A session Cookies Contain the information that is stored in temporary memory location and
then subsequently deleted after the session is completed or the web browser is closed.
The main difference between a session and a cookie is that session data is stored data is
stored on the server whereas cookie store data in the visitor browser.
Session is more secure than cookie as it is stored in server.
64. Enlist and explain the use of any two Intrinsic JavaScript functions.
1) abs() - Returns the absolute value of a number.
2) exp() - Returns E'N Where N is the argument, and E is Euler's Constant, the base of the
natural logarithm.
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 54
CSS/JavaScript Important Questions
65. Write a JavaScript function to check whether a given value is valid IP value or not
<html>
<body>
<script>
var ipaddr = prompt("Enter IP Address:");
var pattern = /^[0-255]{1,3}\.[0-255]{1,3}\.[0-255]{1,3}\.[0-255]{1,3}$/;
// format for ip address range 0.0.0.0 to 255.255.255.255
var result = pattern.test(ipaddr);
if(result)
{
alert("IP Address is Valid");
}
else
{
alert("IP Address is Invalid");
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 55
CSS/JavaScript Important Questions
66. Write a javascript program to validate user accounts for multiple set of user ID and password
(using swith case statement).
<html>
<body>
Enter User ID :<input type="number" id="tf1">
Enter Password: <input type="password" id="tf2">
<input type="button" value="Validate" onclick="validateUser()">
<script>
function validateUser()
{
var userid=document.getElementById("tf1").value;
var psw=document.getElementById("tf2").value;
switch(userid,psw)
{
case 1010,"msbte1": alert("User account is valid");
break;
case 2020,"msbte2": alert("User account is valid");
break;
case 3030,"msbte3": alert("User account is valid");
break;
default: alert("No user details found")
break;
}
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 56
CSS/JavaScript Important Questions
67. Write a javascript program to design HTML page with books information in tabular format,
use rollovers to display the discount information.
<html>
<body>
<table id="bookTable" border="1">
<tr>
<th>BOOKID</th>
<th>NAME</th>
<th>PRICE</th>
<th>DISCOUNT</th>
</tr>
<tr>
<td>1010</td>
<td>C++</td>
<td>550</td>
<td id="discount1">10%</td>
</tr>
<tr>
<td>2020</td>
<td>Java</td>
<td>350</td>
<td id="discount2">15%</td>
</tr>
</table>
<script>
var discount1 = document.getElementById("discount1");
var discount2 = document.getElementById("discount2");
discount1.onmouseover = function()
{
discount1.innerHTML = "Buy one get one free!";
}
discount1.onmouseout = function()
{
discount1.innerHTML = "10%";
}
discount2.onmouseover = function()
{
discount2.innerHTML = "Buy two get one free!";
}
discount2.onmouseout = function()
{
discount2.innerHTML = "15%";
}
</script>
</body>
</html>
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 57
CSS/JavaScript Important Questions
JavaScript IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 58