Assume I\'m a junior Wikipedia user that just want to experiment with changing some wikipedian content with the Wiki text editor in an edit-page, but not saving my changes in an
If you can to know which method is called after clicking the save button, the way below can be helpful:
My suggested way is pressing f12 and then clicking on the save button to find the save method. Now, after finishing dom
loading (in your code), you must replace gained function with an empty function like this:
FOUNDEDSaveFunc = function(){};
If clicking on the save button leads to refresh, you can gain that called action with some apps like fiddler
(instead of pressing f12).
Also, document.addEventListener
adds new listener to current existing listeneres. Because this, the main function that handles keypress, and your added function, will run both (the original save function still will call in this case).
Onclick you could prevent the default action.
document.addEventListener('keypress', function(e) {
if (event.keyCode == 16 && event.keyCode == 18 && event.keyCode == 83) {
e.preventDefault();
}
});
There are a number of problems in your question:
event.key
isn't the same as event.keyCode
, Refer to the documentation.e.key == 16 && e.key == 18 && e.key == 83
will never be true.false
from an event listener doesn't stop the event from being propagated.What you are trying to do can be achieved in the following way:
document.addEventListener("keypress", evt => {
// refer to https://stackoverflow.com/a/2878005/8746648
if(evt.altKey && evt.key == "S") {
alert("prevent this message");
evt.preventDefault();
}
});
// refer to https://stackoverflow.com/a/35611393/8746648
document.addEventListener("keypress", evt => {
if(evt.altKey && evt.key == "S") {
evt.stopPropagation();
}
}, true);
true
in the second event listener.evt.key
is compared with an upper case "s".The keyCodes all represent modifier keys. The keypress
event does not fire with these keys:
document.addEventListener('keypress', function(e) {
console.log('keypress worked');
});
document.addEventListener('keyup', function(e) {
console.log('keyup worked');
});
Also, please note that .keyCode
is deprecated. Should use .key