Professional Documents
Culture Documents
20bce2851 N.babi Da-2
20bce2851 N.babi Da-2
ASSIGNMENT-2
Name: N.BABI
Reg No.: 20BCE2851
Slot: C2
Faculty: Arivoli A
Event Handler
Event handlers can be used to handle and verify user input, user actions, and
browser actions: Things that should be done every time a page loads. Things
that should be done when the page is closed. Action that should be performed
when a user clicks a button.
1. OnLoad – Occurs when the object has been loaded. Onload is most often
used within the <body> element to execute a script once a web page has
completely loaded all content (including images, script files, CSS files, etc.).
The onload event can be used to check the visitor's browser type and browser
version, and load the proper version of the web page based on the
information.
Code:
<html>
<head>
<script language= "JavaScript" type="text/javascript"> function mymessage()
{
alert("This message was triggered from the onload event"); }
</script>
</head>
<body onload="mymessage()">
</body>
</html>
Output:
2. OnUnload
User navigates away from the page. The onunload event occurs once a page
has unloaded (or the browser window has been closed). Note: The onunload
event is also triggered when a user reloads the page (and the onload event).
Code:
<html>
<head>
<script type="text/javascript">
function mymessage()
{
alert("This message was triggered from the onunload event"); }
</script>
</head>
<body onunload="mymessage()">
<p>An alert box will display a message when you close this document!</p>
</body>
</html>
Output:
3. OnChange:
The onchange property of an Input object specifies an eventhandler function
that is invoked when the user changes the value displayed by a form element.
Code:
<HTML>
<TITLE>Example of onChange Event Handler</TITLE>
<HEAD>
<SCRIPT
LANGUAGE="JavaScript">
function valid() {
var input = 0;
input = document.myform.data.value;
alert("You have changed the value from 10 to " + input);
}
</SCRIPT>
</HEAD>
<BODY>
<H3>Example of onChange Event Handler</H3>
Try changing the value from 10 to something else:<br>
<form name="myform">
<input type="text" name="data" value="10" size=10 onChange="valid( )">
</form>
</BODY>
</HTML>
Output:
4. OnSubmit
The onsubmit event occurs when a form is submitted.
Code:
<html>
<head>
<script type="text/javascript">
function confirmInput()
{ fname=document.forms[0].fname.value; alert("Hello " + fname + "! You will
now be redirected to some other page");
}
</script>
</head>
<body>
<form onsubmit="confirmInput()" action="http://www.kcetvnr.org">
Enter your name: <input id="fname" type="text" size="20">
<input type="submit">
</form>
</body>
</html>
Output:
5. Onblur:
The onblur event occurs when an object loses focus. The onblur event is
most often used with form validation code (e.g. when the user leaves a form
field).
Code:
<html>
<head>
<script type="text/javascript">
function message()
{ alert("This alert box was triggered by the onblur event
handler");
}
</script>
</head>
<body>
<p>The onblur event occurs when an element loses focus. Try to click or write
in the input field below, then click elsewhere in the document so the input
field loses focus.</p>
<form>
Enter your name: <input type="text" onblur="message()" size="20">
</form>
</body>
</html>
6. OnFocus:
The onfocus event handler is frequently used with text boxes as well as
select drop-down lists in order to highlight every option that a user is
presently scrolling over to display.
Code:
<html>
<head>
<script type="text/javascript">
function message()
{ alert("This alert box was triggered by the onfocus event
handler");
}
</script>
</head>
<body>
<form>
Enter your name: <input type="text" onfocus="message()" size="20">
</form>
</body>
</html>
Output:
Code:
<html>
<body>
</body>
</html>
8. onmousemove: The onmousemove event occurs when the pointer is
moving while it is over an element.
Code:
<html>
<head>
<script type="text/javascript">
var i=1; function moveright()
{
document.getElementById('header').style.position="relative";
document.getElementById('header').style.left=i; i++;
}
</script>
</head>
<body onmousemove="moveright()">
<h1 id="header">
Move the mouse over this page
</h1>
</body>
</html>
Output:
9. onclick: The onclick event occurs when the user clicks on an element.
Code:
<!DOCTYPE html>
<html>
<body>
<p>Clicking "Copy" triggers a function that copies the text value from Field1
to Field2.</p>
<button onclick="myFunction()">Copy</button>
</body>
</html>
10. onkeydown: The onkeydown event occurs when the user is pressing a key
(on the keyboard).
Code:
<!DOCTYPE html>
<html>
<body>
<p>This example uses the HTML DOM to assign an "onkeydown" event to an input
element.</p>
<p>Press a key inside the text field to set a red background color.</p>
<input type="text" id="demo">
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
</body>
</html>
Output:
11. Onkeypress: The onkeypress event occurs when the user presses a key (on
the keyboard).
Code:
<!DOCTYPE html>
<html>
<body>
<p>This example uses the HTML DOM to assign an "onkeypress" event to an input
element.</p>
<p>Press a key inside the text field to set a red background color.</p>
<input type="text" id="demo">
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
</body>
</html>
12. Onresize: The onresize event occurs when the browser window has been
resized.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This example uses the HTML DOM to assign an "onresize" event to the body
element.</p>
<script>
document.getElementsByTagName("BODY")[0].onresize = function() {myFunction()};
var x =
0;
function myFunction() { var txt = x += 1;
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
Output:
13. Ondrag: The ondrag event occurs when an element or text selection is being
dragged.
Code:
<!DOCTYPE HTML>
<html>
<head>
<style>
.droptarget {
float: left; width:
100px; height: 35px;
margin: 15px; padding:
10px; border: 1px solid
#aaaaaa;
}
</style>
</head>
<body>
<div class="droptarget"></div>
<p id="demo"></p>
<script>
/* Events fired on the drag target */
document.ondragstart = function(event) {
event.dataTransfer.setData("Text", event.target.id);
};
document.ondrag = function(event) {
document.getElementById("demo").innerHTML = "The p element is being dragged";
};
</body>
</html>
Output:
14. Oninput: The oninput event occurs when an element gets user input. This
event occurs when the value of an <input> or <textarea> element is changed.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This example uses the HTML DOM to assign an "oninput" event to an input
element.</p>
</body>
</html>
Output:
15. Onscroll: The onscroll event occurs when an element's scrollbar is being
scrolled.
Code:
<!DOCTYPE html>
<html>
<head> <style> div {
border: 1px solid black;
width: 200px; height:
100px; overflow:
scroll;
}
</style>
</head>
<body>
<p>This example uses the HTML DOM to assign an "onscroll" event to a div
element.</p>
<p id="demo"></p>
<script>
document.getElementById("myDIV").onscroll = function() {myFunction()};
function myFunction() {
document.getElementById("demo").innerHTML = "You scrolled in div.";
}
</script>
</body>
</html>
Output:
16. Onselect: The onselect event occurs after some text has been selected in an
element.
Code:
<!DOCTYPE html>
<html>
<body>
<p>This example uses the HTML DOM to assign an "onselect" event to an input
element.</p>
</body>
</html>
Output:
17. Onstorage: The storage event occurs when there is a change in the
window's change area.
Code:
<!DOCTYPE html>
<html>
<body>
<script> window.addEventListener("storage",
myFunction);
function myFunction(event) {
document.getElementById("demo").innerHTML = "A change was made in the
storage area";
} function changeValue() { var x = window.open("",
"myWindow", "width=200,height=100");
x.localStorage.setItem("mytime", Date.now());
x.close(); }
</script>
</body>
</html>
Output:
18. Onerror: The onerror event is triggered if an error occurs while loading an
external file (e.g. a document or an image).
Code:
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered if an error occurs when loading the image. The
function shows an alert box with a text.
In this example we refer to an image that does not exist, therefore the
onerror event occurs.</p>
<script> function myFunction() { alert('The image could not be loaded.');
}
</script>
</body>
</html>
Output:
19. Onhashchange: The onhashchange event occurs when there has been
changes to the anchor part (begins with a '#' symbol) of the current URL.
Code:
<!DOCTYPE html>
<html>
<body onhashchange="myFunction()">
<p>Click the button to change the anchor part of the current URL to #part5</p>
<button onclick="changePart()">Try it</button>
<p id="demo"></p>
<script>
// Using the location.hash property to change the anchor part function
changePart() { location.hash = "part5"; var x = location.hash;
document.getElementById("demo").innerHTML = "The anchor part is now: " + x;
}
</body>
</html>
Output:
</div>
<script>
function myFunction() { alert("You right-
clicked inside the div!");
}
</script>
</body>
</html>
Output: