如何使用JavaScript禁用右键单击

Web novices often believe that by blocking their visitors' use of the mouse right-click context menu that they can prevent the theft of their web page content. Nothing could be further from the truth.

Web新手通常认为,通过阻止访问者使用鼠标右键单击上下文菜单,可以防止网页内容被盗。 没有东西会离事实很远。

Disabling right clicks is easily sidestepped by more savvy users, and the ability to access much of a web page's code itself is a basic feature of web browsers that doesn't require a right click at all.

精通用户的人很容易回避禁用右键单击,并且访问网页代码本身的能力是Web浏览器的一项基本功能,根本不需要右键单击。

缺点 ( Drawbacks )

There are many ways to bypass the "no right click script," and in reality the only effect that such a script has is to annoy those of your visitors who legitimately use the the right-click context menu (as that menu is properly called) in their web navigation.

有很多方法可以绕过“无右键单击脚本”,实际上,这种脚本的唯一作用是使合法使用右键单击上下文菜单(正确调用该菜单)的访问者烦恼。在他们的网络导航中。

Additionally, all of the scripts that I have seen to do this only block access to the context menu from the right mouse button. They don't consider the fact that the menu is also accessible from the keyboard.

另外,我看到的所有脚本都只能阻止从鼠标右键访问上下文菜单。 他们不认为菜单也可以通过键盘访问。

All anyone needs to do to access the menu using a 104 key keyboard is to select the object on the screen for which they want to access the context menu (for example by left clicking on it) and then press the context menu key on their keyboard—it's the one immediately to the left of the right CTRL key on PC keyboards.

任何人使用104键键盘访问菜单所需要做的就是在屏幕上选择要访问其上下文菜单的对象(例如,通过单击鼠标左键),然后按键盘上的上下文菜单键—它是PC键盘上右CTRL键左侧的一个。

On a 101 key keyboard, you can execute a right-click command by holding down the shift key and pressing F10.

在101键键盘上,可以通过按住Shift键并按F10来执行右键单击命令。

JavaScript ( JavaScript )

If you would like to disable right-clicks on your web page anyway, here's a really simple JavaScript that you can use to block all access to the context menu (not just from the right mouse button but from the keyboard as well)—and really annoy your visitors.

如果您仍然想禁用网页上的右键单击,那么这是一个非常简单的JavaScript ,您可以使用它来阻止对上下文菜单的所有访问(不仅是从鼠标右键,而且是从键盘)-实际上惹恼您的访客。

This script is even simpler than most of the ones that only block the mouse button, and it works in about as many browsers as those scripts do.

这个脚本比大多数只阻止鼠标按钮的脚本更简单,并且在与这些脚本一样多的浏览器中都可以使用。

Here's the entire script for you:

这是您的整个脚本:


<body oncontextmenu="return false;&

Adding just that small piece of code to the body tag of your web page is more effective at blocking your visitor's access to the context menu than the many no-right-click scripts that you can find elsewhere on the web because it blocks access from both the mouse button and from keyboard options described above.

在网页的body标签中仅添加一小段代码,比阻止在网页上其他位置访问的许多无右键单击脚本要有效得多,从而可以阻止访问者访问上下文菜单。鼠标按钮和上述键盘选项。

局限性 ( Limitations )

Of course, the script doesn't work in all web browsers (e.g., Opera ignores it—but then Opera ignores all of the other no-right-click scripts as well).

当然,该脚本并非在所有Web浏览器中都起作用(例如,Opera忽略了该脚本,但是Opera也忽略了所有其他无右键单击的脚本)。

This script also does nothing to prevent your visitors from accessing the page source using the View Source option from their browser menu, or from saving the web page and viewing the source of the saved copy in their favorite editor.

该脚本也无济于事,无法阻止访问者使用其浏览器菜单中的“查看源代码”选项来访问页面源,或者阻止网页保存并在其喜欢的编辑器中查看保存的副本的源。

And finally, though you may disable access to the context menu, that access can be easily re-enabled by users simply by typing

最后,尽管您可以禁用对上下文菜单的访问,但用户只需键入以下内容即可轻松重新启用该访问

javascript:void oncontextmenu(null) into the address bar of the brow

翻译自: https://www.thoughtco.com/how-to-disable-right-clicks-with-javascript-4071868

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值