CSS QB
CSS QB
ii) clearInterval()
The clearInterval() method stops the executions of the function specified in the
setInterval() method.
Syntax: window.clearInterval(timerVariable)
The window.clearInterval() method can be written without the window prefix.
8. What is rollover
Ans: Rollover means a webpage changes when the user moves his or her mouse over an object
on the page. It is often used in advertising. There are two ways to create rollover, using plain
HTML or using a mixture of JavaScript and HTML. We will demonstrate the creation of
rollovers using both methods. The keyword that is used to create rollover is the event. For
example, we want to create a rollover text that appears in a text area. The text “What is
rollover?” appears when the user place his or her mouse over the text area and the rollover text
changes to “Rollover means a webpage changes when the user moves his or her mouse over an
object on the page” when the user moves his or her mouse away from the text area.
4 Marks Questions
1. Describe the navigator object in JavaScript. Describe the methods of navigator object which
is used to display browser name and version.
Ans:
2. Write a JavaScript function to check whether a given address is a valid IP address or not.
Ans:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP Address Validator</title>
</head>
<body>
<h2>IP Address Validator</h2>
<input type="text" id="ipInput" placeholder="Enter IP address">
<button onclick="validateIP()">Check</button>
<p id="result"></p>
<script>
function validateIP() {
const ip = document.getElementById("ipInput").value;
const ipv4Pattern = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]
[0-9]?)$/;
if (ipv4Pattern.test(ip)) {
document.getElementById("result").innerText = "Valid IPv4 address.";
} else {
document.getElementById("result").innerText = "Invalid IPv4 address.";
}
}
</script>
</body>
</html>
3. List Ways of protecting your web page and describe any one of them.
Ans: There is nothing secret about your web page. Anyone with a little computer knowledge can
use a few mouse clicks to display your HTML code, including your JavaScript, on the screen.
Following are the ways to protect web pages: 1) Hiding Your Code by disabling Right Mouse
Click: The following example shows you how to disable the visitor's right mouse button while
the browser displays your web page. All the action occurs in the JavaScript that is defined in the
tag of the web page.
<html>
<head>
<script>
window.onload = function()
{
document.addEventListener("contextmenu", function(e)
{
e.preventDefault();
}, false);}
</script>
<body>
<h3>Right click on screen,Context Menu is disabled</h3>
</body>
</html>
The preventDefault() method cancels the event if it is cancelable, meaning that the default action
that
belongs to the event will not occur.
For example, this can be useful when:
Clicking on a "Submit" button, prevent it from submitting a form
Clicking on a link, prevent the link from following the URL
2) Hiding JavaScript
You can hide your JavaScript from a visitor by storing it in an external file on your web server.
The
external file should have the .js file extension. The browser then calls the external file whenever
the
browser encounters a JavaScript element in the web page. If you look at the source code for the
web page,
you'll see reference to the external .js file, but you won't see the source code for the JavaScript.
webpage.html
<html>
<head>
<script src="mycode.js" languages="javascript" type="text/javascript">
</script>
<body>
<h3> Right Click on screen, Context Menu is disabled</h3>
</body>
</html>
mycode.js
window.onload=function()
{
document.addEventListener("contextmenu", function(e)
{
e.preventDefault();
}, false);
}
4. Write a JavaScript that accepts the string and searches for the pattern “MSBTE” in the given
string using regular expression. If the pattern is found JavaScript will display “Pattern is
found” else display “pattern is not found.”
Ans:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pattern Search</title>
</head>
<body>
<h2>Pattern Search for "MSBTE"</h2>
<input type="text" id="inputText" placeholder="Enter text">
<button onclick="searchPattern()">Check Pattern</button>
<p id="result"></p>
<script>
function searchPattern() {
const text = document.getElementById("inputText").value;
const pattern = /MSBTE/; // Regular expression to search for "MSBTE"
if (pattern.test(text)) {
document.getElementById("result").innerText = "Pattern is found";
} else {
document.getElementById("result").innerText = "Pattern is not found";
}
}
</script>
</body>
</html>
5. Write a JavaScript program that will create a pull-down menu with three options. Once the
user will select the one of the options then the user will redirected to that website.
Ans:
Ans:
<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="select any option">Select</option>
<option value="https://www.codecademy.com/catalog/language/javascript/">
CodeAcademy </option>
<option value="https://www.msbte.org.in">MSBTE</option>
<option value="https://www.javatpoint.com/javascript-tutorial">JavaTpoint</option>
</form>
</body>
</html>
6. With the help of example describe how to replace text using a regular expression.
Ans:
you can also use a regular expression to replace portions of the text by using the
replace() method.
The replace() method requires two parameters: a regular expression and the
replacement text.
Here's how the replace() method works. First, you create a regular expression that
identifies the portion of the text that you want replaced.
Then you determine the replacement text. Pass both of these to the replace()
method, and the browser follows the direction given in the regular expression to
locate the text. If the text is found, the browser replaces it with the new text that
you provided.
The replace() method searches a string for a specified value, or a regular exp
Syntax:
string.replace(searchvalue, newvalue)
Example:
<html>
<script>
function check()
{
var exp=/:/g;
var str=document.getElementById("txt").value;
var res=str.replace(exp,"-");
document.getElementById("demo1").innerHTML=res;
}
</script>
<body>
Enter text:<textarea id="txt"></textarea>
<input type="button" onclick="check()" value="check">
<p id="demo1"></p>
</body> </html>
7. List and state various properties of a window object. Write a JavaScript that opens a new
popup window with message “WELCOME To SCRIPTING” when the page loads and a new
popup window displaying message “FUN WITH SCRIPTING” when the page unloads
Ans:
Property Description
Document It returns the document object for the window (DOM).
Frames It returns an array of all the frames including iframes in the
current window.
Closed It returns the Boolean value indicating whether a window has
been closed or not.
History It returns the history object for the window.
innerHeig It sets or returns the inner height of a window's content area.
ht
innerWidt It sets or returns the inner width of a window's content area.
h
Length It returns the number of frames in a window.
Location It returns the location object for the window.
Name It sets or returns the name of a window.
Navigator It returns the navigator object for the window.
Opener It returns a reference to the window that created the window.
outerHeig It sets or returns the outer height of a window, including
ht toolbars/scrollbars.
outerWidt It sets or returns the outer width of a window, including
h toolbars/scrollbars.
Parent It returns the parent window of the current window.
Screen It returns the screen object for the window.
screenX It returns the X coordinate of the window relative to the screen.
screenY It returns the Y coordinate of the window relative to the screen.
Self It returns the current window.
Status It sets the text in the status bar of a window.
Top It returns the topmost browser window that contains frames.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Popup Example</title>
<script>
// Function to display popup on page load
function welcomePopup() {
window.open("", "", "width=300,height=200").document.write("<h3>WELCOME To
SCRIPTING</h3>");
}
Ans:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laptop Brand Display</title>
</head>
<body>
<h2>Laptop Brands</h2>
9. When the mouse moves over the specific brand name the script must display the image of
respective Laptop in the adjacent box.
Ans:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laptop Brand Display</title>
</head>
<body>
<h2>Laptop Brands</h2>
<script>
function showImage(imgPath) {
// Change the src attribute of the image element to show the new image
document.getElementById('laptopImg').src = imgPath;
}
</script>
</body>
</html>
Ans:
<!DOCTYPE html>
<html>
<head>
<title>Frameset Layout</title>
</head>
<frameset rows="30%, 70%">
<!-- Frame 1 on the top row -->
<frame src="frame1.html" name="frame1" title="Frame 1">
<!-- Bottom row divided into three frames: Frame 2, Frame 4, Frame 3 -->
<frameset cols="33%, 34%, 33%">
<frame src="frame2.html" name="frame2" title="Frame 2">
<frame src="frame4.html" name="frame4" title="Frame 4">
<frame src="frame3.html" name="frame3" title="Frame 3">
</frameset>
</frameset>
<noframes>
<body>
<p>Your browser does not support frames.</p>
</body>
</noframes>
</html>
13. Write HTML script that will display dropdown list containing options such as Red, Green, Blue &
Yellow. Write a JavaScript program such that when the user selects any options. It will change the
background colour of webpage.
Ans:
<html>
<body>
<label for="color">Choose a Background Color:</label>
<select name="color" id="color" class="color" onchange="changeColor()">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
</select>
<script type="text/javascript">
function changeColor() {
var color = document.getElementById("color").value;
switch(color){
case "green":
document.body.style.backgroundColor = "green";
break;
case "red":
document.body.style.backgroundColor = "red";
break;
case "blue":
document.body.style.backgroundColor = "blue";
break;
case "yellow":
document.body.style.backgroundColor = "yellow";
break;
default:
document.body.style.backgroundColor = "white";
break;
}
}
</script>
</body>
</html>
14. Write a javascript to open a new window and the new window is having two frames. One frame
containing buthon as “click here !”, and after clicking this button an image should open in the second
frame of that child window.
Ans:
f.html
<html>
<head>
<title>Create a Frame</title>
</head>
<frameset rows="60%,40%">
</frameset>
</frameset>
</html>
b. html
<html>
<head>
</head>
<body>
</body>
</html>
im.html
<html>
<head>
</head>
<body>
<img src="i.png">
</body>
</html>