What is the main difference between of using this...
document.addEventListener('mousedown', function() {
// code
}, false);
...and this?
document.onmousedown = function() {
// code
}
Will there be any different result or any cause?
The addEventListener() method makes it easier to control how the event reacts to bubbling. When using the addEventListener() method, the JavaScript is separated from the HTML markup, for better readability and allows you to add event listeners even when you do not control the HTML markup.
There's no difference; it's just different terminology for the same thing. There are different ways of associating functions with DOM elements for the purpose of event handling, that's all.
addEventListener() has multiple advantages: Allows you to register unlimited events handlers and remove them with element. removeEventListener() . Has useCapture parameter, which indicates whether you'd like to handle event in its capturing or bubbling phase.
Basically, there is no difference between using a document and a window. You can use any of those as per your preference. Some functions like a scroll and resize should be available in the window. addEventListener.
onclick
is a property, like the onclick
attribute can be placed in HTML. It has best browser support, however, it is primitive, as reassigning it overwrites the first (like any object property).
addEventListener()
, as the name suggests, allows you to register multiple callbacks for an element and event type. This allows you to have multiple mousedown
events for the same element. Before IE9, IE had their own attachEvent()
which is similar (you must specify the on
part too with attachEvent()
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With