Short QB CSS-II 22-23 Solved
Short QB CSS-II 22-23 Solved
2 Marks Questions
Chapter – IV
1. State any two properties and methods of location object.
Location Object Properties
Property Description
2.Enlist and explain the use of any two Intrinsic JavaScript functions.
•They are the in built functions which can be used with JavaScript objects
•Various objects supported by JavaScript include document, window, Date , Math , Array, String etc.
•The javascript provides the intrinsic functions for submit or reset button. One can use these
functionalities while submitting the form or resetting the form fields.
• The submit() method of the form object can be used to send the form to the server in exactly same way
as id the user has pressed the submit button.
1. getDate() :- This method returns the day of the month for the specified date
according to local time.
syntax: object.getDate();
2. max():-This method returns the largest of zero or more numbers.
Syntax:-max(obj1,obj2);
<html>
<body>
<form name="myform">
Roll No:<input type="text" name="roll"/>
<br/><br/>
Name :<input type="text" name="name"/>
<br/><br/>
<im src="submit.gif" onclick="javascript.document.forms.myform.submit()"/>
<br/><br/>
</form>
</body>
</html>
<html>
<body>
<form name="myform">
Roll No:<input type="text" name="roll"/>
<br/><br/>
Name :<input type="text" name="name"/>
<br/><br/>
<im src="submit.gif" onclick="javascript.document.forms.myform.submit()"/>
<br/><br/>
</form>
</body>
</html>
b) Permanent cookies
• Permanent cookies, also known as 'persistent cookies', remain in operation even after the web browser
has closed.
• For example, they can remember login details and passwords so web users don't need to re-enter them
every time they use a site.
• The law states that permanent cookies must be deleted after 12 months.
6. Construct regular expression for validating the phone number in following format
only : (nnn)-nnnn-nnnn OR nnn.nnnn.nnnn
<html>
<head><title>Validation</title>
<script language="javascript">
function validate()
{
var mobile = document.myform.mobile.value;
var regex = /^\d{4}\.\d{4}\.\d{4}$|^\d{4}\-\d{4}\-\d{4}$/g
if(!regex.test(mobile))
{
alert("invalid mobile number");
}
else
{
alert("Valid mobile number");
}
}
</script>
</head>
<body>
<form name="myform" onSubmit="validate()">
Enter username : <input type="text name="user"><br><br>
Enter Mobileno : <input type="text" name="mobile"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<script>
function display()
{
parent.frame2222.document.getElementById("para").innerHTML="Element of frame
2 is accessed from frame1";
}
</script>
</head>
<body>
<h4>Frame 1</h4>
<input type="button" name="button1" value="Click" onclick="display()"/>
</body>
</html>
<html>
<body>
<h4>Frame 2</h4>
<p id="para">Frame 2 content</p>
</body>
</html>
Chapter – VI
9. Write a JavaScript program that create a scrolling text on the status line of a window.
We can show a message scrolling in status bar by assigning the spaces and the message to the status bar
in time interval
<html>
<head>
<title>Scrolling Text</title>
<script language="JavaScript">
var scrollPos = 0
var maxScroll = 100
var blanks = ""
function displayText(text) {
window.defaultStatus = blanks + text
++scrollPos
blanks += " "
if(scrollPos > maxScroll) {
scrollPos = 0
blanks = ""
}
}
</script>
</head>
<body onload="scrollText('Watch this text scroll!!!', 300)">
<p>Watch the text scroll at the bottom of this window!</p>
</body>
</html>
• In the above program window.setInterval("displayText('"+text+"')",milliseconds) is used to set the
scrolling message.
• The setinterval() function is used to set the message after milliseconds.
• The text will send to the function displaytext() function, this function is called after milliseconds defined
in setinterval() function.
• After calling the displaytext() function, the statement window.defaultStatus=blank + text is executed.
• In which the status of the window is changed by setting the new text to defaultstatus property of
window object
10. Develop a JavaScript Program to Create Rotating Banner Ads with URL Links. OR
Write a JavaScript program to link banner advertisements to different URLs.
<html>
<head>
<script language="Javascript">
MyBanners=new Array('banner1.jpg','banner2.jpg','banner3.jpg','banner4.jpg')
MyBannerLinks=new
Array('http://www.amazon.in/','http://www.flipkart.in/','http://aliexpress.in
/','http://olx.in/')
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 onload="ShowBanners()">
<center>
<a href="javascript: ShowLinks()">
<imgsrc="banner1.jpg" width="900" height="120" name="ChangeBanner"/></a>
</center>
</body>
</html>
11. 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 MouseButton
3.Hiding JavaScript
4.Concealing E-mail address.
There are 2 ways of protecting webpage:
1. Disabling right click: The source code of a web page can be viewed by clicking right mouse button on
the webpage. Anyone can see the source code of a webpage which is not safe. We can hide the code
by disabling right mouse click on webpage. Hiding source code is nothing but protecting the source
code by viewing other users.
2. Concealing email address:It means hiding email address from unauthorized user. It is possible with the
use of JavaScript.
JavaScript for concealing email address
function (user_email)
{
varavg, splitted, part1, part2;
splitted = user_email.split("@");
part1 = splitted[0];
avg = part1.length / 2;
part1 = part1.substring(0, (part1.length - avg));
part2 = splitted[1];
alert(part1 + "...@" + part2);
};
4 Marks Questions
Chapter – IV
12. Describe, how to read cookie value and write a cookie value. Explain with example.
OR
Write a JavaScript program to create read, update and delete cookies.
• JavaScript can create, read, and delete cookies with the document.cookie property.
• With JavaScript, a cookie can be created like this:
document.cookie = "username=student";
• You can also add an expiry date (in UTC time). By default, the cookie is deleted when the browser is
closed:
document.cookie="username=student;expires=Thu,18 Dec2013 12:00:00 UTC";
• With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie
belongs to the current page.
document.cookie = "username=student; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
• 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.
• 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:
Example:
<html >
<head>
<title>Open New Window</title>
<script >
function OpenNewWindow() {
MyWindow = window.open(‘webpage1.html’, 'myAdWin', 'status=0, toolbar=0,
location=0,
menubar=0, directories=0, resizable=0, height=250, width=250')
}
</script>
</head>
<body>
<FORM action=" " method="post">
<INPUT name="OpenWindow" value="Open Window" type="button"
onclick="OpenNewWindow()"/>
</FORM>
</body>
</html>
14. Write HTML Script that displays textboxes for accepting Name, middle name, Surname of the user
and a Submit button. Write proper JavaScript such that when the user clicks on submit button
a. all texboxes must get disabled and change the color to “RED”. and with respective labels.
b. Constructs the mailID as <name>.<surname>@msbte.com 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>
<head>
<script>
function a()
{
document.getElementById("fname").disabled=true;
document.getElementById("mname").disabled=true;
document.getElementById("sname").disabled=true;
fname.style.backgroundColor = "red";
mname.style.backgroundColor = "red";
sname.style.backgroundColor = "red";
var firstName = fname.value;
var lastName = sname.value;
var email = firstName +"."+ lastName + "@msbte.com";
alert(email);
}
</script>
</head>
<body>
<form id="myform">
Enter First Name:
<input type="text" id="fname"><br>
Enter Middle Name:
<input type="text" id="mname"><br>
Enter Sur Name:
<input type="text" id="sname"><br>
<input type="button" value="Submit"
onclick="a()"><br>
</form>
</body>
</html>
15. Write a webpage that displays a form that contains an input for Username and password. User is
prompted to enter the input and password and password becomes value of the cookie. Write The
JavaScript function for storing the cookie . It gets executed when the password changes.
<html>
<head><title>Cookie</title>
<script language="javascript">
function store()
{
var user = document.form1.user.value
var pass = document.form1.pass.value
var cookieinfo = "username="+user+",password="+pass;
document.cookie = cookieinfo
alert(document.cookie + " Stored");
}
</script>
</head>
<body>
<form name="form1" onsubmit="store()">
Enter username <input type="text" name="user">
Enter password <input type="password" name="pass" onchange="store()">
<input type="submit" value="Submit">
</form>
</body>
</html>
Chapter – V
16. State what is regular expression. Explain its meaning with the help of a suitable example.
• A regular expression is a sequence of characters that forms a search pattern.
• When you search for data in a text, you can use this search pattern to describe what you are
searching for.
• A regular expression can be a single character, or a more complicated pattern.
• Regular expressions can be used to perform all types of text search and text replace operation
Syntax
/pattern/modifiers;
Example
var patt = /w3schools/I
Example explained:
• /w3schools/i is a regular expression.
• w3schools is a pattern (to be used in a search).
• i is a modifier (modifies the search to be case-insensitive).
Modifiers
Modifiers are used to perform case-insensitive and global searches:
Modifier Description
g Perform a global match (find all matches rather than stopping after the first match)
17. Write a JavaScript program to validate email ID of the user using regular expression.
<html>
<body>
<script>
function validateemail()
{
var x=document.myform.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n
dotposition:"+dotposition);
return false;
}
}
</script>
<body>
<form name="myform" method="post"
action="http://www.javatpoint.com/javascriptpages/valid.jsp" onsubmit="return
validateemail();">
Email: <input type="text" name="email"><br/>
Creating Rollover
• To create rolloer we use ’onmouseover’ event.
• When the mouse pointer is moved onto an element, onto one of its children, the mouseover element is
occurred.
• The onmouseover event is generally used with the ‘onmouseout’.
• When the mouse pointer is moved out of the element, the onmouseout element is occurred.
<html>
<body>
<p
onmouseover=”this.style.color=’red'”
onmouseout=”this.style.color=’blue'”>
Move the mouse over this text to change its color to red. Move the mouse away
to
change the text color to blue.
</p>
</body>
</html>
19. Write a JavaScript program to create rollover effect for three images.
• You create a rollover for text by using the onmouseover attribute of the <A> tag, which is the anchor
tag.
• You assign the action to the onmouseover attribute the same way as you do with an <IMG> tag.
• Let's start a rollover project that displays a flower titles.
• Additional information about a flower can be displayed when the user rolls the mouse cursor over the
flower name.
• In this example, the image of the flower is displayed. However, you could replace the flower image
with an advertisement or another message that you want to show about the flower.
<html>
<body>
<h1>Image Rollover</h1>
<a><IMG src='apples.gif' height="92" width="70" border="0"
onmouseover="src='apples.gif' "
onmouseout="src='redstar.gif' "></a>
<a><IMG src='ND1.png' height="92" width="70" border="0"
onmouseover="src='ND1.png' "
onmouseout="src='M1.png' "></a>
<a><IMG src='M1.png' height="92" width="70" border="0" onmouseover="src='M1.png'
"
onmouseout="src='B.png' "></a>
</body>
</body>
</html>
Chapter – VI
20. Create a slideshow with the group of three images, also simulate next and previous transition
between slides in your Java Script.
<html>
<head>
<script>
pics = new Array('1.jpg' , '2.jpg' , '3.jpg'); count = 0;
function slideshow(status)
{
if (document.images)
{
count = count + status;
if (count > (pics.length - 1))
{
count = 0;
}
if (count < 0)
{
count = pics.length - 1;
}
documet.imag1.src = pics[count];
}
}
</script>
</head>
<body>
<img src="1.jpg" width="200" name="img1">
<br>
<input type="button" value="Next" onclick="slideshow(1)">
<input type="button" value="Back" onclick="slideshow(-1)">
</body>
</html>
21. 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>
<head>
<title>HTML Form</title>
<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"; optionList[2].value=3;
}
}
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
<p>
<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>
<input name="Reset" value="Reset" type="reset">
</p>
</form>
</body>
</html>
22. 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>
<head>
<title>HTML Form</title>
<script language="javascript" type="text/javascript"> function getPage(choice)
{
page=choice.options[choice.selectedIndex].value; if(page != "")
{
window.location=page;
}
}
</script>
</head>
<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>
</form>
</body>
</html>