Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to disable right click on asp.net web page using JavaScript | Disable mouse right click on webpage

Oct 30, 2011
Introduction

Here I will explain how to disable right click on website using JavaScript.

Description

One day I got requirement like do not allow users to right click on website to see source code of page for that I written JavaScript functionality to disable right click on website to achieve this create new website and design your Default.aspx page like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sample to Disable Right Click of Page</title>
<script language="JavaScript" type="text/javascript">
//Message to display whenever right click on website
var message = "Sorry, Right Click have been disabled.";
function click(e) {
if (document.all) {
if (event.button == 2 || event.button == 3) {
alert(message);
return false;
}
}
else {
if (e.button == 2 || e.button == 3) {
e.preventDefault();
e.stopPropagation();
alert(message);
return false;
}
}
}
if (document.all) {
document.onmousedown = click;
}
else {
document.onclick = click;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Demo



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

16 comments :

Dheeraj Bansal said...

Thanks Again !!!
This code really works as charm for me.

yaswanth kumar said...

hello! i need code to disable all the controls which able to copy the data in PDF file when opened in WEBSITE!!

PLZ.....MAKE it !!

Hazrat said...

Dear sir i m new to your website.Really very very usable and good.I like this site.

But sir i want know that one, whatever you have wrote for disabling right Click on a asp page, it works on IE only,
But i want to know what have to do for other Browser to disable rt clk.plz do the needfull.Thanks in adv.

besthavenki said...

thanking you very much

SaudiTamilan said...

This is useful script by suresh.One more thing is,write this script in .js file and save it in solution explorer 'script' folder and call it from Master page.

vicky said...

How to disable file upload & file download options using c#.net

Unknown said...

Thanks ...It is works

Anonymous said...

it's not working for me.... pls help

Suresh Burdak said...

Thanks Suresh!!!

Its working fine.....

Unknown said...

Not working at all on chrome...

Anonymous said...

your site is very helpful.. Thanks a lot.
Sir i am trying to block save page as option in the menu of FF & IE web browser so the images, script do not get downloaded when user click save page as html format... I hv disable the right click menu, but still users can access the sources page from the main menu..

Kindly give your suggestion/advice on it, how can i do it

Suryakant Chouhan said...

Its working for IE not for Chrome...

Abhi Abhi said...

Hi,

It is working only in IE.
Please show for all browsers.

God said...

Chutye... key bord se right click karega to view source hota hai..

Anonymous said...

Thank you Suresh, You provoide us many necessary things.
Use...(oncontextmenu="return false;") in body or any images or anywhere , where you don't want Right click.
Like:->

Anonymous said...

it's not working

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.