1

I'm blocking context menu, then I want to restore it to previous state.

myElement = document.querySelector('*');
myElement.addEventListener('contextmenu', MyContextMenu);

it is possible to restore default context menu after the above code have been executed ? if the answer is yes then how or how to make it correctly?

what i want is to block context menu and then restore it after a while.

  • Not sure if i understand your question correctly, but you could call `myElement.removeEventListener('contextmenu', MyContextMenu);` at the time where you don't want to have `MyContextMenu` to be called anymore for the next invocations of the contextmenu. – t.niese Nov 06 '13 at 22:40

3 Answers3

4

Reassign all context menus to default:

document.querySelector('div').oncontextmenu = _=>false;

var d = document.createElement('div').oncontextmenu;
[...document.querySelectorAll("*")].forEach(e => e.oncontextmenu = d);
<div>sample</div>
Travis J
  • 81,153
  • 41
  • 202
  • 273
1
var oldHandlerToKeep = element.oncontextmenu
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
  • it is possible to pass it as parameter as: `myElement.addEventListener('contextmenu', MyContextMenu(myElement.oncontextmenu));` –  Nov 06 '13 at 22:38
  • if I can pass old handler to new handler then this answer is pretty enough for me –  Nov 06 '13 at 22:39
  • This would only work if the other handler was set with `element.oncontextmenu = AnotherHandler;`, but not if it was registered with `element.addEventListener('contextmenu', AnotherHandler);`. Anyway as long as `MyContextMenu` does not return `false`, stops propagation or prevents the default behavior, the other handlers (including the default on) should also be called, because `addEventListener` does not overwrite the existing handlers, but just adds an additional one. – t.niese Nov 06 '13 at 22:47
  • well then I ***guess*** setting is to NULL would mean getting back to the standard one – Trident D'Gao Nov 06 '13 at 22:52
  • doing `element.oncontextmenu = null;` would not remove a listener added with `element.addEventListener('contextmenu', ... )`. – t.niese Nov 06 '13 at 22:58
  • well, then it means that the default handler is not exposed at all, it should work then by just calling removeEventListener passing whatever handler was assigned via addEventHandler – Trident D'Gao Nov 06 '13 at 23:42
-1

Here's an alternate solution (based on this post by Chema):

    document.body.oncontextmenu = null;
    document.addEventListener("contextmenu",
        function (event) {
            event.returnValue = true;
            if (typeof(event.stopPropagation) === 'function')
            {
                event.stopPropagation();
            }
            if (typeof(event.cancelBubble) === 'function')
            {
                event.cancelBubble();
            }
        }, true);

This helps to restore the context menu if preventDefault() was previously called. Hopefully it might help some passersby.

JK.
  • 21,477
  • 35
  • 135
  • 214
user16118981
  • 101
  • 1