There are two scripts in a document:
// my_script.js goes first
document.onclick = function() {
alert("document clicked");
};
// other_script.js comes after
// this overrides the onclick of my script,
// and alert will NOT be fired
document.onclick = function() {
return false;
};
To make sure my click
event does not get overridden by other script, I switched to addEventListener
.
// my_script.js goes first
document.addEventListener("click", function() {
alert("document clicked");
}, false);
// other_script.js comes after
document.addEventListener("click", function() {
return false;
}, false);
Now I got another question. Since return false
in the second code is defined after alert
, how come it does not prevent alert from being called?
What if I want my script to get total control of click event (like return false
all the time disregarding events defined in other scripts)?