Search code examples
javascriptcontextmenuaddeventlistener

Re-enabling right-click on web page: "Void document oncontextmenu null" trick not working


For testing purposes, I have disabled the right click function on a web page by adding the following JavaScript snippet code:

document.addEventListener('contextmenu', event => event.preventDefault());

So far, so good.

Now, to re-enable the option using a code method, it is supposed to be enough to just type javascript:void(document.oncontextmenu=null); in the address bar of your Browser and press Enter. But this method seems not be working anymore. I do not know what is wrong with it and wonder if there is any similar alternative way, except toggling off JavaScript in Chrome?


Solution

  • addEventListener() method can be removed with removeEventListener() method only

    If you need your code working you need to assign the event with the oncontextmenu property

    I mean that

    document.oncontextmenu = (event) => {
      event.preventDefault();
    }
    

    It will work.