I would like to disable undo and redo in all browsers using JavaScript. I want to do this as our app has it's own undo and redo history built in.
I can already handle key presses to disable undo and redo via a keyboard entry but I would like to disable the menu items as well. (Standard menu and right click menu)
Is this possible? Even if it is only possible in some browsers it would be better than none.
I know this is old, but for others that are curious, this worked for me.
$(window).bind('keydown', function(evt) {
if((evt.ctrlKey || evt.metaKey) && String.fromCharCode(evt.which).toLowerCase() == 'z') {
evt.preventDefault();
}
});
One thing you can do is intercept the popstate event. Unfortunately i don't think it can be canceled, so you have to hack it up a bit by adding a dummy state to undo to.
The following code prevented me from pressing back on Chrome and FF. Feel free to test it on other browsers.
window.setTimeout(function(){
window.history.pushState({}, '', '#');
window.addEventListener("popstate", function() {
window.history.pushState({}, '', '#');
});
},1000);
Make sure to change your code as appropriate to fit in with your own history. Also consider using the HTML5 History API instead of your own history implementation.
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
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