Disable context menu

 To prevent the default context menu from appearing on right-click in your website, you can use JavaScript to capture the right-click event and prevent its default action. Here's a simple example using JavaScript:


```html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Disable Right-Click Menu</title>

    <style>

        /* Optional: some styling to show the area where right-click is disabled */

        body {

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

            margin: 0;

        }

        .content {

            padding: 20px;

            border: 2px solid #000;

        }

    </style>

</head>

<body>

    <div class="content">

        Right-click is disabled on this page.

    </div>


    <script>

        document.addEventListener('contextmenu', function(event) {

            event.preventDefault();

            alert('Right-click is disabled.');

        });

    </script>

</body>

</html>

```


In this example, the `contextmenu` event is captured using `addEventListener`, and `event.preventDefault()` is called to prevent the default context menu from appearing. You can replace the `alert` with any custom functionality you want to execute on right-click.


Feel free to adjust the script according to your needs, such as applying it to specific elements instead of the whole document. For instance, if you only want to disable the context menu on a specific element, you can do something like this:


```html

<script>

    document.querySelector('.content').addEventListener('contextmenu', function(event) {

        event.preventDefault();

        alert('Right-click is disabled on this element.');

    });

</script>

```

Previous
Next Post »