最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable Undo and Redo in browsers - Stack Overflow

programmeradmin0浏览0评论

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 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.

Share Improve this question edited May 16, 2013 at 3:59 Aran Mulholland asked May 16, 2013 at 2:23 Aran MulhollandAran Mulholland 23.9k30 gold badges145 silver badges234 bronze badges 1
  • 1 The only way I'd know would be to detect a change in a field and check if it was not caused by keyboard input, cut/paste, or your own scripts, then assume that it was the undo/redo function that did so. As far as I am aware, there is no working event handler to catch undo/redo events, so (if you really need it) this might be the only option. – Qantas 94 Heavy Commented May 16, 2013 at 2:41
Add a ment  | 

4 Answers 4

Reset to default 4

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/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 patible with html5 browsers.

Using my library undo.js, you can disable undo and redo actions (not just from shortcuts) using the following code:

undo.observe(document.body, {
    preventDefault:true
});

You can then capture requests from the user for undo and redo actions (using a shortcut or the browser menu) using:

myeditor.addEventListener("undo", function(e){
    // whatever...
});
myeditor.addEventListener("redo", function(e){
    // whatever...
});
发布评论

评论列表(0)

  1. 暂无评论