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

PPTX
DOM Events
PDF
Introducing jQuery
KEY
Events
PDF
JavaScript - Chapter 11 - Events
PPT
Document_Object_Model_in_javaScript..................................ppt
PPTX
Dom(document object model)
PPTX
JavaScript_Events.pptx
PPTX
Web programming
DOM Events
Introducing jQuery
Events
JavaScript - Chapter 11 - Events
Document_Object_Model_in_javaScript..................................ppt
Dom(document object model)
JavaScript_Events.pptx
Web programming

Similar to Javascript Browser Events.pdf (20)

PPTX
Event In JavaScript
PPTX
Events - Part 2
PPTX
Upstate CSCI 450 WebDev Chapter 9
PDF
JavaScript From Scratch: Events
PPTX
High performance websites session1
PPTX
Jquery introduce
PPTX
Unit ii java script and xhtml documents and dynamic documents with javascript
PDF
JavaScript DOM & event
PDF
20180921 #24 we_are_javascripters
ODP
Event handling using jQuery
PPTX
JavaScript patterns chapter 8 of mine
PPTX
DOM_and_JS_Events_Presentation_simpleandShort.pptx
PPTX
Javascript 2
PPTX
Event loop in browser
PDF
The Fine Art of JavaScript Event Handling
KEY
jQuery Events
PPTX
FYBSC IT Web Programming Unit III Events and Event Handlers
PDF
Fast Cordova applications
PPTX
Client Web
PDF
Web 5 | JavaScript Events
Event In JavaScript
Events - Part 2
Upstate CSCI 450 WebDev Chapter 9
JavaScript From Scratch: Events
High performance websites session1
Jquery introduce
Unit ii java script and xhtml documents and dynamic documents with javascript
JavaScript DOM & event
20180921 #24 we_are_javascripters
Event handling using jQuery
JavaScript patterns chapter 8 of mine
DOM_and_JS_Events_Presentation_simpleandShort.pptx
Javascript 2
Event loop in browser
The Fine Art of JavaScript Event Handling
jQuery Events
FYBSC IT Web Programming Unit III Events and Event Handlers
Fast Cordova applications
Client Web
Web 5 | JavaScript Events
Ad

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
System and Network Administration Chapter 2
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Digital Strategies for Manufacturing Companies
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Nekopoi APK 2025 free lastest update
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Reimagine Home Health with the Power of Agentic AI​
System and Network Administration Chapter 2
CHAPTER 2 - PM Management and IT Context
Odoo POS Development Services by CandidRoot Solutions
Digital Strategies for Manufacturing Companies
Upgrade and Innovation Strategies for SAP ERP Customers
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
VVF-Customer-Presentation2025-Ver1.9.pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Softaken Excel to vCard Converter Software.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ai tools demonstartion for schools and inter college
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
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