How do I disable right click on my web page?
You can do that with JavaScript by adding an event listener for the "contextmenu" event and calling the preventDefault()
method:
document.addEventListener('contextmenu', event => event.preventDefault());
That being said: DON'T DO IT.
Why? Because it achieves nothing other than annoying users. Also many browsers have a security option to disallow disabling of the right click (context) menu anyway.
Not sure why you'd want to. If it's out of some misplaced belief that you can protect your source code or images that way, think again: you can't.
How to disable mouse right click on a web page?
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
How to disable right click menu in html?
Assuming this for a web app...
I also had to do this and used Javascript
document.addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);
Should do the trick
Thanks
disable right click without js and css
It is done with javascript.
I have disabled javascript and I can right-click. It disables with:
<script language="JavaScript">
document.oncontextmenu =new Function("return false;")
</script>
<body onselectstart="return false">
There might be an issue with your browser, the site is online for me.
Disable right click WITHOUT an alert-box (Javascript)
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function() {
window.event.returnValue = false;
});
}
Answer based on How to add a custom right-click menu to a webpage?
How to disable right click on whole website (not just a few pages) in Angular
Working demo in this Stackblitz Demo Link
Basically need to access document object inside angular service using injection token of document, so that its safe way to access dom using document inside angular. below is service code
export class DisableRightClickService {
constructor(@Inject(DOCUMENT) private document: Document) {}
disableRightClick() {
this.document.addEventListener('contextmenu', (event) =>
event.preventDefault()
);
}
}
In above code we are adding contextmenu event to document and at the same time we prevent event to occur too!. Now, need to add this inside app.component.ts file like below..
export class AppComponent {
constructor(private rightClickDisable: DisableRightClickService) {}
ngOnInit() {
this.rightClickDisable.disableRightClick();
}
}
This way you can disable right click for your whole application.
How to disable right-click in Javascript?
window.oncontextmenu = function() {return false;}
However, some browsers prevent disabling and changing the context menu, so it's not reliable.
Related Topics
Download File Using JavaScript/Jquery
Wait Until All Promises Complete Even If Some Rejected
Remove Empty Elements from an Array in JavaScript
Rails 4: How to Use $(Document).Ready() With Turbo-Links
When Are You Supposed to Use Escape Instead of Encodeuri/Encodeuricomponent
Innertext' Works in Ie, But Not in Firefox
Window.Onload VS Document.Onload
Using the Variable "Name" Doesn't Work With a Js Object
JavaScript Set Object Key by Variable
React Setstate Not Updating State
How to Get Jquery to Perform a Synchronous, Rather Than Asynchronous, Ajax Request
JavaScript Equivalent of Python'S Zip Function
Convert String to Title Case With JavaScript
How to Get Started With Node.Js
Jquery: Return Data After Ajax Call Success
Escaping HTML Strings With Jquery