How to disable right click using Javascript and Jquery

Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article, I will explain how to disable right click on the  web page by using Javascript and Jquery.

Disable Right Click in HTML by using JavaScript

In JavaScript, document.oncontextmenu is an event handler property that allows you to execute a function when the contextmenu event work on the entire document. In the event handler of oncontextmenu will be the cancelled by returning FALSE value. The contextmenu event is typically triggered when a user right click with the mouse  on an element to open the context menu, or when the context menu key is pressed.

<script type="text/javascript">

    //Disable the Context Menu event.

     document.oncontextmenu =  function () {

        return false;

    };

</script>

 

Disable Right Click in HTML by using jQuery

On the HTML page I inherited jQuery file named jquery.min.js.

In JavaScript, document.oncontextmenu is an event handler property that allows you to execute a function when the contextmenu event occurs on the document object. In the event handler of concontextmenu will be the cancelled by returning FALSE value.The contextmenu event is typically fired when the user right click by mouse on an element with the intention of opening a context menu.

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<script type="text/javascript">

    //Disable the Context Menu event.

    $(document).contextmenu(function () {

        return false;

    });

</script>

 

Disable Right Click in HTML using JavaScript and jQuery

You will need to place the below mentioned script in HEAD tag section of the HTML page.

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    // This function will work for Disable Mouse right click by Using JavaScript

    <script type="text/javascript">

        //Disable the Context Menu event.

         document.oncontextmenu =  function () {

            return false;

        };

    </script>

 

    // This function will work for Disable Mouse right click by Using jQuery

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        //Disable the Context Menu event.

        $(document).contextmenu(function () {

            return false;

        });

    </script>

</head>

<body>

    <form id="form1" runat="server">

        <h1> Please Try to Right Click</h1>

    </form>

</body>

</html>

Below I am giving the complete code for this program by using Jquery. You need to create a HTML page named Using_jQuery.You can copy and paste the whole code on your HTML page to enjoy the execution of this program.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <style type="text/css">

        body {

            font-family: Arial;

            font-size: 10pt;

        }

    </style>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        //Disable the Context Menu event.

        $(document).contextmenu(function () {

            return false;

        });

    </script>

</head>

<body>

    <h1>Please Try to Right Click.</h1>

</body>

</html>

Below I am giving the complete code for this program by using Javascript. You need to create a HTML page named Using_javascript. You can copy and paste the whole code on your HTML page to enjoy the execution of this program.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <style type="text/css">

        body {

            font-family: Arial;

            font-size: 10pt;

        }

    </style>

    <script type="text/javascript">

        //Disable the Context Menu event.

        document.oncontextmenu = function () {

            return false;

        };

    </script>

</head>

<body>

   <h1>Please Try to Right Click.</h1>

</body>

</html>

Conclusion: In above code, I have been explained that how to disable right click on the  web page using Javascript and Jquery. This code is very helpful for every developer. Bye Bye and take care of you Developers. We will come back shortly with the new article.

Thanks and Regards

Using ASP.Net

 

 

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net