cs511 Week 7
cs511 Week 7
Event handling
In every language of computer science event has its own significance, but in JavaScript, which is
utilized in web development, it is very significant. An event is an action that occurs as a result of
the user or another source, such as a mouse click. Let’s see how it is handled in JavaScript.
Introduction:
There are multiple ways to handle an event. Let’s have a look at them.
Figure no.80.1
The very first line portrays that a JavaScript code has been stored in an external file which
includes the some functions such as
} // highlight text
The figure 80.1 clearly depicts the events on which these functions are invoked.
Similarly, we may define several events on a single object using this method.
The key benefit of this method is that JavaScript code is segregated from HTML. The script/code
may be in an external file or an embedded JavaScript. The code of HTML will be very clean and
clear, while making changes in HTML you need not worry about the functionality and vice versa.
Event Object
When an event is triggered, the browser will construct an event object that contains information
about the event.
topic 82
apturing and Bubbling.
Event Handling:
o Events are handled in three phases.
o Capturing
Event propagation from parent towards the most nested child.
o Target
Identifying the target.
o Bubbling
Elements registered for the events are triggered from the most
nested child towards the outermost.
In JavaScript, events progress from the immediate object that caused the event to the document
root. This is referred to as event propagation or event bubbling.
While this may appear to be a difficult concept, the underlying notion is extremely
straightforward. When you click on an element, you're also clicking on all the ascendants in the
DOM tree, as seen in Figure 82.1
Figure 82.1
Week 83
Event Types – mouse, keyboard and touch.
Figure taken from: Fundamentals of Web Development (Second Edition, Pearson), Authors: Randy
Connolly, Ricardo Hoar
The click event is perhaps the most visible, but JavaScript and the DOM support a variety of others. In
reality, the W3C specifies various classes of event, each having multiple types of events inside each class.
Mouse events, keyboard events, touch events, form events, and frame events are some of the most
popular event types.
Let’s have a look at few types of events which are supported by JavaScript.
Mouse events are used to describe a variety of mouse-driven interactions. Mouse click and mouse move
events are two types of mouse events. Many mouse events can be sent at once, which is unusual. The
user may move the mouse from one div to another at the same time, activating the mouseon and
mouseout events as well as the mousemove event. To deal with these difficulties, use the Cancelable
and Bubbles properties. Following is the list of mouse events.
And mouseup is used only for up and likewise mousedown is used only for down.
Within input fields, these events are most useful. With each key hit, we could, for example, validate an
email address or submit an asynchronous request for a dropdown list of suggestions with each key press.
<input type=“text” id=“key”>
For this input box, we could listen for key press events and replay each pushed key back to the user as
shown below.
var keyPressed=e.keyCode;
// get the raw key code
var character=String.fromCharCode(keyPressed);
// convert to string
});
Note: Regrettably, different browsers handle keyboard properties differently. For example, the keyCode
property is not available for the keypress event in FireFox. If we used the same callback method for the
keypress event in code above, we'd have to update the code to retrieve the key press as follows:
Instead of clogging our code with these kind of browser testing conditional statements, we can use
something like the jQuery framework to handle these quirks.
Touch events: these are relatively new events which are especially used in mobile devices.
Touch events are a relatively new category of events that can be triggered by devices with touch
screens.
The different events (e.g., touchstart, touchmove, and touchend) are analogous to some of the
mouse events (mousedown, mousemove, and mouseup).
Touch events are only available by default in Chrome and iOS Safari at the time of writing.
In Edge and FireFox, the user must allow touch events. Pointer events, a new standard
specification that combines mouse, touch screen, and pen input into a single event type, are
supported by Microsoft Edge. However, pointer events are only supported in Edge at the time of
writing.
<div>
<label> Name</label>
<input type = “text” id = “name”>
</div>
<div>
<label> Email</label>
</div>
Figure 83.1
//comment out the div that was built with the event listener as shown in figure 83.2
Figure 83.2
<script>
uname.addEventListener(‘keypress’, function(e){console.log(e.keycode)
if (e.keycode==95)
e.preventdefault();} )
The event listener is working fine, because we get the ASCII code for the keys which are being pressed,
and when the key code is equal to 95 then prevent the default behavior, it implies that the user is not
allowed to type underscore in the input box as shown in figure 83.3,
Week 84
Form validation is the process of determining whether or not the user has filled out the form correctly.
This activity can be done at various stages. E.g
HTML Level
// using built-in HTML element we can do validation e.g type of an email address must be email
likewise specify the range for a specific number.
JavaScript Level
Note: the user may disable JavaScript. And submit the form. Because similar situations may arise,
it is vital to validate the form at the server level as well to prevent erroneous data from being
stored in the database. So it good to have validation at all the stages.
Webserver Level
Form Events:
Events related to form are called form events. Forms are the primary means of collecting and
transmitting user input to the server. Form events allow us to perform some real-time processing in
response to user input. The submit event is the most popular JavaScript listener for forms. We use
preventDefault() to prevent submitting to the server and inform the user if the password field (with id
pw) is blank. Otherwise, nothing is done, allowing the default event to occur (submitting the form).
document.getElementById(“loginForm”).addEventListener('submit', function(e)
Form events
submit// When the form is submitted this event is triggered. We can do some prevalidation of
the
reset // HTML forms have the ability to be reset. This event is triggered when that happens.
formdata
Textfield events
blur // Triggered when a form element has lost focus (that is, control has moved to a different
change// some <input>, <textarea> or <select> field had their value change. This could mean
focus // Complementing the blur event, this is triggered when an element gets focus (the user
select // When the users selects some text. This is often used to try and prevent copy/paste.
….
Let's look at an example where these events will be used to validate a form.
function check_name () {
if (this.value.length<5)
} else {
function check_email () {
if (! re.test (this.value)){
} else {
e.preventDefault ();
}
Week 85
Introduction to jQuery
JQuery is one of the JavaScript libraries that are used to assist us with web development.
jQuery is a popular framework that gives developers access to cross-browser animation tools, user
interface elements, and DOM manipulation functions, among other things. Learning about frameworks,
especially jQuery, can help you improve your development abilities by allowing you to use ever-evolving
frameworks that provide new and increasingly expected functionality.
How to include JQuery in our code, there are multiple ways to include it.
Including JQuey
(CDN)
o
<script src = https://code.jquery.com/jquey-3.6.0.min.js> </script>
//can include directly from CDN, min.js is best as it has minimum size.
Can also be
Note: JQuery can be included in the head, or before body in HTML file.
Let's have a look at an example that how it can be included in HTML file.
Figure 85.1 shows that how it can be included when we have an internet connection.
Figure no 85.1
But if we have no internet connection then download and save it in folder where you have HTML file.
Week 86