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;
}
document.oncontextmenu = function () { return false; };
</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. |
|||
|
|||
19 comments :
Thanks Again !!!
This code really works as charm for me.
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 !!
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.
thanking you very much
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.
How to disable file upload & file download options using c#.net
Thanks ...It is works
it's not working for me.... pls help
Thanks Suresh!!!
Its working fine.....
Not working at all on chrome...
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
Its working for IE not for Chrome...
Hi,
It is working only in IE.
Please show for all browsers.
Chutye... key bord se right click karega to view source hota hai..
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:->
it's not working
NOT WORKING
Thanks for notifying me i made change now it's working fine check it..... Happy coding
It really helped me a lot..Thank u so much Suresh!!
Note: Only a member of this blog may post a comment.