Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Undo and Redo in browsers

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.

like image 208
Aran Mulholland Avatar asked May 16 '13 02:05

Aran Mulholland


2 Answers

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();
  }
});
like image 190
Chris R Avatar answered Nov 06 '22 06:11

Chris R


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

like image 35
Tibos Avatar answered Nov 06 '22 08:11

Tibos