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

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

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Apr 23, 2022 at 19:04 Vanda Nojan - وندا نوژنVanda Nojan - وندا نوژن 451 gold badge1 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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.

Right-click anywhere on the page (if allowed) and select Inspect or press Ctrl + Shift + I (Cmd + Option + I on Mac) to open the developer tools. Go to the Console tab. Type or paste the following JavaScript mand and press Enter:

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论