AWDT module 2
AWDT module 2
<!doctype html>
<html>
<head>
<script>
function hiThere() {
alert('Hi there!');
}
</script>
</head>
<body>
<button type="button"
onclick="hiThere()"
style="margin-left: 50%;">
Click me event
</button>
</body>
</html>
Output:
<body>
<h4>The input box will change color when UP arrow key is
pressed</h4>
<input id="bg" onkeyup="changeBackground()" placeholder="write something"
style="color:#fff">
</body>
</html>
Output:
As shown in the above figure, when a user clicks a specific mouse button or types a specific
keyword into the browser, that action activates the corresponding event handler for that
HTML element. The browser then shows the end users the effects of the actions that were
carried out on the webpage by the JavaScript code that was executed by the event handler.
Event handlers can be assigned directly using the equal (=) operator because they are
attributes of HTML/DOM elements as well
For registering handlers, there are two recommended methods. By assigning the event
handler code to the target element's corresponding on event property or by adding the handler
as a listener for the element using the addEventListener() method, the event handler code can
be made to execute when an event is triggered. The handler will receive an object that
complies with the Event interface in either scenario (or a derived interface). The main
difference is that using the event listener methods, additional event handlers can be added (or
removed).
• Capturing Phase: In the capture phase, generally known as the trickling phase, the event
"trickles down" to the element that caused the event.
• Target Phase: It starts with the element and handler at the top level and works its way down
to the element. When the event arrives at the target, the capture phase is over.
• Bubbling Phase: The event is "bubbled" up to the DOM tree during the bubble phase. The
innermost handler initially captures and handles it (the one that is closest to the element on
which the event occurred). After that, it moves up (or bubbles up) to the DOM tree's higher
levels, moves up to its parents, and eventually returns to its root.
They follow the same order as listed above. As shown in the diagram below:
DOCUMENT
HTML
BODY
DIV
BUTTON
In event handling, when an event reaches the element, it enters the capturing phase. Events
enter the target phase when they reach the element, and they up from the element during
the bubbling phase.
Event Objects
Event handler functions accept an argument, the event object, even though we haven't used it
up to this point. Additional information about the event is also included in this object. For
example, we can check the event object's button property to verify which mouse button was
pressed.
Event
Description
Handler
When a click action occurs on an HTML element, this event handler runs a
JavaScript script. For example, the onClick event handler may be called when a
Onclick
button is clicked, a link is clicked, a checkbox is checked, or an image map is
selected.
When the mouse is over a particular link or object, this event handler runs a
onmouseover
JavaScript script.
When a window or image has fully loaded, this event handler executes some
Onload
JavaScript code.
onkeypress When a user presses a key, this event handler executes JavaScript code.
When the mouse leaves a specific link or object, this event handler runs a
onmouseout
JavaScript script.
When a key is released during a keyboard action, this event handler executes
Onkeyup
JavaScript code.
When a key is pressed on the keyboard during an action, this event handler runs
onkeydown
a JavaScript script.
At predetermined intervals,
setInterval() calls a function or
window.setInterval()
evaluates an expression (in
milliseconds).
• frame1.html
<!DOCTYPE html>
<html>
<body>
<h3>frame 1</h3>
</body>
</html>
• frame2.html
<!DOCTYPE html>
<html>
<body>
<h3>frame 2</h3>
</body>
</html>
• frame3.html
<!DOCTYPE html>
<html>
<body>
<h3>frame 3</h3>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<frameset cols="*,*,*">
<frame src="frame1.html">
<frame src="frame2.html">
<frame src="frame3.html">
</frameset>
</body>
</html>
Output:
column frames
<html>
<body>
<frameset rows="*,*,*">
<frame src="frame1.html">
<frame src="frame2.html">
<frame src="frame3.html">
</frameset>
</body>
</html>
Output:
JavaScript Form Validation
•
JavaScript Form Validation is a way to ensure that the data users enter into a
form is correct before it gets submitted. This helps ensure that things like
emails, passwords, and other important details are entered properly, making
the user experience smoother and the data more accurate.
Steps for Form Validation in JavaScript
When we validate a form in JavaScript, we typically follow these steps:
• Data Retrieval:
o The first step is to get the user’s values entered into the form
fields (like name, email, password, etc.). This is done using
document.forms.RegForm, which refers to the form with the
name “RegForm”.
• Data Validation:
o Name Validation: We check to make sure the name field isn’t
empty and doesn’t contain any numbers.
o Address Validation: We check that the address field isn’t empty.
o Email Validation: We make sure that the email field isn’t empty
and that it includes the “@” symbol.
o Password Validation: We ensure that the password field isn’t
empty and that the password is at least 6 characters long.
o Course Selection Validation: We check that a course has been
selected from a dropdown list.
• Error Handling:
oIf any of the checks fail, an alert message is shown to the user
using window.alert, telling them what’s wrong.
o The form focuses on the field that needs attention, helping the
user easily fix the error.
• Submission Control:
o If all the validation checks pass, the function returns true,
meaning the form can be submitted. If not, it returns false,
stopping the form from being submitted.
• Focus Adjustment:
o The form automatically focuses on the first field that has an error,
guiding the user to fix it.
• The following script listening shows you how create your own.
• <html>
• <head>
• <title>JavaScript Image Rollovers</title></head>
• <body>
• <a href="link.html"
• onMouseOver="document.image1.src='onImage.gif'"
• onMouseOut="document.image1.src='outImage.gif'">
•
• <img src="outImage.gif" name="image1">
•
• </a>
• </body>
• </html>
• This is the easiest rollover there
is. onMouseOver="document.image1.src='onImage.gif'" replaces “outImage.gif”
with
“onImage.gif”. onMouseOut="document.image1.src='outImage.gif'" swapps
“outImage.gif” back when the user moves his mouse away from it.
• static
• relative
• fixed
• absolute
• sticky
Elements are then positioned using the top, bottom, left, and right
properties. However, these properties will not work unless
the position property is set first. They also work differently depending on the
position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
properties.
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
Try it Yourself »
position: relative;
An element with position: relative; is positioned relative to its normal
position.
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
Try it Yourself »
ADVERTISEMENT
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which
means it always stays in the same place even if the page is scrolled. The top,
right, bottom, and left properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally
have been located.
Notice the fixed element in the lower-right corner of the page. Here is the
CSS that is used:
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
Try it Yourself »
This <div> element has position: fixed;
position: absolute;
An element with position: absolute; is positioned relative to the nearest
positioned ancestor (instead of positioned relative to the viewport, like fixed).
Note: Absolute positioned elements are removed from the normal flow, and
can overlap elements.
Example
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
Try it Yourself »
position: sticky;
An element with position: sticky; is positioned based on the user's scroll
position.
A sticky element toggles between relative and fixed, depending on the scroll
position. It is positioned relative until a given offset position is met in the
viewport - then it "sticks" in place (like position:fixed).
1. setTimeout (function, duration): This function can be used to call the function after a
millisecond delay.
2. setInterval (function, duration): This function can be used to call the function after each
duration milliseconds.
3. clearTimeout (setTimeout_variable): This function can be used to clear the timer that
has been set by the setTimeout()
JavaScript can also modify a DOM object"s attributes, such as its location on the screen.
An object"s top and left attributes can be set to place it anywhere on the frame. The syntax
of JavaScript may be defined as: