Home > Back-end >  How to disable right click on whole website (not just a few pages) in Angular
How to disable right click on whole website (not just a few pages) in Angular

Time:12-12

I have tried to search for solutions which would allow me to disable right click on the whole application in Angular 2 but I get only solutions for disabling on few components like,

<someSelector appDisableRightClick></someSelector>

where, someSelector is any element of HTML, appDisableRightClick is a directive to disable right click using hostlistener for contextmenu event.

The problem with this is it will have to be written everywhere which means if I want to disable right click everywhere in my application, this is not something I should be looking for. Need a little help here please for the optimum solution to achieve this for the whole application.

CodePudding user response:

As commented by @GRD, the below link provides a good and acceptable solution for the problem. Asked him to put comment in answers section, but he didn't, need to close this question, so putting it myself here.

https://stackblitz.com/edit/angular-ivy-pvmcrz?file=src/app/app.component.ts

CodePudding user response:

There are a few techniques to achieve this,

  1. HTML way,
<body oncontextmenu="return false">
  1. CSS way,
your-class {
  pointer-events: none;
}
  1. HTML and JS ways,
<script language="JavaScript">
    document.oncontextmenu =new Function("return false;")
</script>    
<body onselectstart="return false">
  • Related