简体   繁体   中英

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.

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

it is not possible using browser apis. but there is a few ways to detect or handling methods. url hashing is a one way to handling back or forward event. however browsers not support this for user safety. you can search javascript url hashing scripts but do not forget about that scripts compatible with html5 browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM