SlideShare a Scribd company logo
Introduction to Browser
Events
Shubham Chaurasia (schaurasia@)
AGENDA
● What is Event?
● Event Handlers and Listeners
● Event Object
● DOM Event Architecture
● Event Phases
● Multiple Event Listener
● Event Delegation
● Conclusion
What is Event?
● An action that is fired (initiated) within a web page.
● In browsers everything starts by following an event.
● JavaScript uses asynchronous callback.
● The event could be the DOM is loaded(window.onload), or an asynchronous request
that finishes fetching(xhr.onreadystatechange), or a user clicking an element or
scrolling the page, or the user types on the keyboard.
What is Event?
Event Handler
You can respond to any event using an Event Handler, which is a function that’s called when
an event occurs.
There are 3 ways to register event handlers
● Inline event handlers (Old way)
<a href="site.com" onclick="dosomething();">A link</a>
Will be tedious job if you want to attach the events to 100 anchor tags.
● DOM on-event handlers (Easy way)
var el = document.getElementById(‘foo’);
el.onclick = doSomething;
This is common when an object has at most one event handler, as there is no way to
add multiple handlers in this case. but better cross-browser compatibility ( IE )
Event Handler
Event Handler
● Using addEventListener and attachEvent (Right way)
var el = document.getElementById(‘foo’);
el.addEventListener(‘click’, doSomething);
el.attachEvent(‘click’, doSomething); ( In Internet Explorer )
This is the modern way and follows w3c standard. This method allows to register as
many handlers as we need.
You can use removeEventListener and detachEvent(IE) to remove the event handler.
This isn't significant for simple, small programs, but for larger, more complex programs it
can improve efficiency to clean up old unused event handlers.
This allows you to have the same button performing different actions in different
circumstances — all you've got to do is add/remove event handlers as appropriate.
You can also register multiple handlers for the same listener.
Event Object
● Contains contextual information about the current event.
● An object that’s provided, or is available, inside of every event handler function.
● W3C Standard Browser: e
● Internet Explorer: window.event
● This object contains a lot of useful properties and methods, like:
○ target, the DOM element that originated the event
○ currentTarget
○ type
○ stopPropagation()
○ preventDefault()
○ path and many more
Event Object
DOM Event Architecture
DOM Event Architecture
Two Models
Not surprisingly, back in the bad old days Netscape and Microsoft came to different
conclusions.
1. Netscape said that the event on element1 takes place first. This is called event capturing.
2. Microsoft maintained that the event on element2 takes precedence. This is called event
bubbling.
DOM Event Architecture
attachEvent(‘click’, handler)
addEventListener(‘click’, handler, true);
Event Capturing
Event Bubbling
W3C has very sensibly decided to take a middle position in this struggle. Any event taking
place in the W3C event model is first captured until it reaches the target element and then
bubbles up again.
W3C Model
W3C DOM Event Architecture
IE events are stacked
element.attachEvent('onclick', function(){ alert(‘1’); })
element.attachEvent('onclick', function(){ alert(‘2’); })
W3C events are queued
element.addEventListener('click', function(){ alert(‘1’); })
element.addEventListener('click', function(){ alert(‘2’); })
Multiple Event Listener
Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is
added to one parent. That event listener analyzes bubbled events to find a match on child elements.
Let’s also say that something needs to happen when each child element is clicked. You could add a separate
event listener to each individual LI element, but what if LI elements are frequently added and removed from
the list? Adding and removing event listeners would be a nightmare, especially if addition and removal code
is in different places within your app. The better solution is to add an event listener to the parent UL element
Event Delegation
Conclusion
Thank You!

More Related Content

Similar to Javascript Browser Events.pdf (20)

PPTX
Event In JavaScript
ShahDhruv21
 
PPTX
Events - Part 2
alexisabril
 
PPTX
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
PDF
JavaScript From Scratch: Events
Michael Girouard
 
PPTX
High performance websites session1
amr elgarhy
 
PPTX
Jquery introduce
Major Ye
 
PPTX
Unit ii java script and xhtml documents and dynamic documents with javascript
zahid7578
 
PDF
JavaScript DOM & event
Borey Lim
 
PDF
20180921 #24 we_are_javascripters
将一 深見
 
ODP
Event handling using jQuery
Iban Martinez
 
PPTX
JavaScript patterns chapter 8 of mine
Chien-Wei Huang
 
PPTX
DOM_and_JS_Events_Presentation_simpleandShort.pptx
shivaninegi0435
 
PPTX
Javascript 2
pavishkumarsingh
 
PPTX
Event loop in browser
valuebound
 
PDF
The Fine Art of JavaScript Event Handling
Yorick Phoenix
 
KEY
jQuery Events
nek4life
 
PPTX
FYBSC IT Web Programming Unit III Events and Event Handlers
Arti Parab Academics
 
PDF
Fast Cordova applications
Ivano Malavolta
 
PPTX
Client Web
Markiyan Matsekh
 
PDF
Web 5 | JavaScript Events
Mohammad Imam Hossain
 
Event In JavaScript
ShahDhruv21
 
Events - Part 2
alexisabril
 
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
JavaScript From Scratch: Events
Michael Girouard
 
High performance websites session1
amr elgarhy
 
Jquery introduce
Major Ye
 
Unit ii java script and xhtml documents and dynamic documents with javascript
zahid7578
 
JavaScript DOM & event
Borey Lim
 
20180921 #24 we_are_javascripters
将一 深見
 
Event handling using jQuery
Iban Martinez
 
JavaScript patterns chapter 8 of mine
Chien-Wei Huang
 
DOM_and_JS_Events_Presentation_simpleandShort.pptx
shivaninegi0435
 
Javascript 2
pavishkumarsingh
 
Event loop in browser
valuebound
 
The Fine Art of JavaScript Event Handling
Yorick Phoenix
 
jQuery Events
nek4life
 
FYBSC IT Web Programming Unit III Events and Event Handlers
Arti Parab Academics
 
Fast Cordova applications
Ivano Malavolta
 
Client Web
Markiyan Matsekh
 
Web 5 | JavaScript Events
Mohammad Imam Hossain
 

Recently uploaded (20)

PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Ad

Javascript Browser Events.pdf

  • 1. Introduction to Browser Events Shubham Chaurasia (schaurasia@)
  • 2. AGENDA ● What is Event? ● Event Handlers and Listeners ● Event Object ● DOM Event Architecture ● Event Phases ● Multiple Event Listener ● Event Delegation ● Conclusion
  • 4. ● An action that is fired (initiated) within a web page. ● In browsers everything starts by following an event. ● JavaScript uses asynchronous callback. ● The event could be the DOM is loaded(window.onload), or an asynchronous request that finishes fetching(xhr.onreadystatechange), or a user clicking an element or scrolling the page, or the user types on the keyboard. What is Event?
  • 6. You can respond to any event using an Event Handler, which is a function that’s called when an event occurs. There are 3 ways to register event handlers ● Inline event handlers (Old way) <a href="site.com" onclick="dosomething();">A link</a> Will be tedious job if you want to attach the events to 100 anchor tags. ● DOM on-event handlers (Easy way) var el = document.getElementById(‘foo’); el.onclick = doSomething; This is common when an object has at most one event handler, as there is no way to add multiple handlers in this case. but better cross-browser compatibility ( IE ) Event Handler
  • 7. Event Handler ● Using addEventListener and attachEvent (Right way) var el = document.getElementById(‘foo’); el.addEventListener(‘click’, doSomething); el.attachEvent(‘click’, doSomething); ( In Internet Explorer ) This is the modern way and follows w3c standard. This method allows to register as many handlers as we need. You can use removeEventListener and detachEvent(IE) to remove the event handler. This isn't significant for simple, small programs, but for larger, more complex programs it can improve efficiency to clean up old unused event handlers. This allows you to have the same button performing different actions in different circumstances — all you've got to do is add/remove event handlers as appropriate. You can also register multiple handlers for the same listener.
  • 9. ● Contains contextual information about the current event. ● An object that’s provided, or is available, inside of every event handler function. ● W3C Standard Browser: e ● Internet Explorer: window.event ● This object contains a lot of useful properties and methods, like: ○ target, the DOM element that originated the event ○ currentTarget ○ type ○ stopPropagation() ○ preventDefault() ○ path and many more Event Object
  • 12. Two Models Not surprisingly, back in the bad old days Netscape and Microsoft came to different conclusions. 1. Netscape said that the event on element1 takes place first. This is called event capturing. 2. Microsoft maintained that the event on element2 takes precedence. This is called event bubbling. DOM Event Architecture
  • 15. W3C has very sensibly decided to take a middle position in this struggle. Any event taking place in the W3C event model is first captured until it reaches the target element and then bubbles up again. W3C Model
  • 16. W3C DOM Event Architecture
  • 17. IE events are stacked element.attachEvent('onclick', function(){ alert(‘1’); }) element.attachEvent('onclick', function(){ alert(‘2’); }) W3C events are queued element.addEventListener('click', function(){ alert(‘1’); }) element.addEventListener('click', function(){ alert(‘2’); }) Multiple Event Listener
  • 18. Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent. That event listener analyzes bubbled events to find a match on child elements. Let’s also say that something needs to happen when each child element is clicked. You could add a separate event listener to each individual LI element, but what if LI elements are frequently added and removed from the list? Adding and removing event listeners would be a nightmare, especially if addition and removal code is in different places within your app. The better solution is to add an event listener to the parent UL element Event Delegation