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!
Ad

More Related Content

Similar to Javascript Browser Events.pdf (20)

Event
EventEvent
Event
mussawir20
 
PATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelPATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event Model
Michael Heron
 
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWTEVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
JavaScript patterns chapter 8 of mine
JavaScript patterns chapter 8 of mineJavaScript patterns chapter 8 of mine
JavaScript patterns chapter 8 of mine
Chien-Wei Huang
 
DOM Events
DOM EventsDOM Events
DOM Events
Peter Frueh
 
Ext Js Events
Ext Js EventsExt Js Events
Ext Js Events
jason hu 金良胡
 
Ext Js Events
Ext Js EventsExt Js Events
Ext Js Events
jason hu 金良胡
 
Eurosec2014 - An introduction to honeyclient technologies
Eurosec2014 - An introduction to honeyclient technologiesEurosec2014 - An introduction to honeyclient technologies
Eurosec2014 - An introduction to honeyclient technologies
Angelo Dell'Aera
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
Faisal Aziz
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
greenrobot
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
WPF Fundamentals
WPF FundamentalsWPF Fundamentals
WPF Fundamentals
Our Community Exchange LLC
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
Java script browser objects 1
Java script browser objects 1Java script browser objects 1
Java script browser objects 1
H K
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
PERKYTORIALS
 
Attribute actions
Attribute actionsAttribute actions
Attribute actions
Matthew Beale
 
Java gui event
Java gui eventJava gui event
Java gui event
SoftNutx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
Air Drag And Drop
Air Drag And DropAir Drag And Drop
Air Drag And Drop
michael.labriola
 
PATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelPATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event Model
Michael Heron
 
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWTEVENT DRIVEN PROGRAMMING SWING APPLET AWT
EVENT DRIVEN PROGRAMMING SWING APPLET AWT
mohanrajm63
 
JavaScript patterns chapter 8 of mine
JavaScript patterns chapter 8 of mineJavaScript patterns chapter 8 of mine
JavaScript patterns chapter 8 of mine
Chien-Wei Huang
 
Eurosec2014 - An introduction to honeyclient technologies
Eurosec2014 - An introduction to honeyclient technologiesEurosec2014 - An introduction to honeyclient technologies
Eurosec2014 - An introduction to honeyclient technologies
Angelo Dell'Aera
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
Faisal Aziz
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
greenrobot
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
Java script browser objects 1
Java script browser objects 1Java script browser objects 1
Java script browser objects 1
H K
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
PERKYTORIALS
 
Java gui event
Java gui eventJava gui event
Java gui event
SoftNutx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 

Recently uploaded (20)

Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World ExamplesMastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??
Web Designer
 
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEMGDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
philipnathen82
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Gojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service BusinessGojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service Business
XongoLab Technologies LLP
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Adobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 linkAdobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 link
mahmadzubair09
 
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with PrometheusMeet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Eric D. Schabell
 
sequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineeringsequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineering
aashrithakondapalli8
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World ExamplesMastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??
Web Designer
 
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEMGDS SYSTEM | GLOBAL  DISTRIBUTION SYSTEM
GDS SYSTEM | GLOBAL DISTRIBUTION SYSTEM
philipnathen82
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Adobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 linkAdobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 link
mahmadzubair09
 
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with PrometheusMeet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Eric D. Schabell
 
sequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineeringsequencediagrams.pptx software Engineering
sequencediagrams.pptx software Engineering
aashrithakondapalli8
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
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
  翻译: