How to disable mouse right click on a web page? [d

2019-02-03 03:00发布

This question already has an answer here:

I want to disable mouse right click on an HTML page. I have a page where user has to enter the details. I don't want the user to see the menu thats get displayed with the mouse right click. Rather I want to display a custom menu. I know there are some plugins available to do that. But my requirement does not need any plugins.

10条回答
可以哭但决不认输i
2楼-- · 2019-02-03 03:20

Please do not do that, it is very annoying.

The right menu is there for a reason, and it should be left there. Many browser extensions add entries to the right click menu and the user should be able to use it in any page he visits.

Moreover you can use all of the functionality of the right click menu in other ways (keyboard shortcuts, browser menu etc etc etc) so blocking the right click menu has the only effect of annoying the user.

PS: If really you cannot resist the urge to block it at least do not put a popup saying "no right click allowed".

查看更多
SAY GOODBYE
3楼-- · 2019-02-03 03:22

Firstly, if you are doing this just to prevent people viewing the source of your page - it won't work, because they can always use a keyboard shortcut to view it.

Secondly, you will have to use JavaScript to accomplish this. If the user has JS disabled, you cannot prevent the right click.

That said, add this to your body tag to disable right clicks.

<body oncontextmenu="return false;">
查看更多
地球回转人心会变
4楼-- · 2019-02-03 03:23

It's unprofessional, anyway this will work with javascript enabled:

document.oncontextmenu = document.body.oncontextmenu = function() {return false;}

You may also want to show a message to the user before returning false.

However I have to say that this should not be done generally because it limits users options without resolving the issue (in fact the context menu can be very easily enabled again.).

The following article better explains why this should not be done and what other actions can be taken to solve common related issues: http://articles.sitepoint.com/article/dont-disable-right-click

查看更多
在下西门庆
5楼-- · 2019-02-03 03:27
window.oncontextmenu = function () {
return false;
}

might help you.

查看更多
We Are One
6楼-- · 2019-02-03 03:29

Try this : write below code on body & feel the magic :)

body oncontextmenu="return false"
查看更多
劫难
7楼-- · 2019-02-03 03:35

<body oncontextmenu="return false;"> works for me in Google Chrome. Not sure about other browsers.

Note, all someone has to do is disable JavaScript in order to see the right-click menu anyway.

查看更多
登录 后发表回答