Site icon Sibeesh Passion

How to disable right click in a page using JQuery

Introduction

In this post, we will see how we can disable right click option using simple JQuery code. I hope you will like it.

Background

Most of the developers are always looking for the safety of their images and code blocks which they have shared in the web. But normally it is hard to secure this unless you go for any third party tool. Some of us may think if we just disable the right click option in our page, makes the content secured. But it is not. If some one really thinks that they need to get the content from your page, they can achieve it. Even though that is the reality, I am sharing this post to show how to disable the right click option in page.

Using the code

We are going to use JQuery for this requirement. So you need to load the JQuery reference first.

[js]
<script src="http://sibeeshpassion.com/content/scripts/jquery-1.11.1.min.js"></script>
[/js]

Now we need to add the needful scripts.

[js]
<script>
$(document).ready(function() {
$(document).on("contextmenu",function(e){
return false;
});
});
</script>
[/js]

Here what we did is, whenever user tries to perform right click in the document, we are restricting that. We are applying this restriction to the entire document. And we use contextmenu event to do this requirement.

That is all. Everything is done and set. Now you need to see the output and demo right?

Demo

You can see a demo at Disable Right Click Option Demo

You can see right click option is not allowed in the demo page. You can always gives an alert also, if a user performs right click.

Conclusion

I hope you liked my article. Now please share me your feedback. Thanks in advance.

Kindest Regards
Sibeesh Venu

Exit mobile version