Powered by Blogger.
advertisement

Saturday 5 March 2016

Disable Mouse Right Click using JavaScript

Advertisement
advertisement

1:   Disable Mouse Right Click using JavaScript

Disable Mouse Right Click using JavaScript : Today we are show you how to disable mouse right click using JavaScript in website or asp.net webpage. It’s a very simple script to disable right click event. You can disable the right click event in whole website by adding this script in master page.
In previous articles we explained Get Directions Google Map API using JavaScriptGet Computer Hardware InformationStatic Website in ASP.NETAdding Multiple Points to Google Map,Send Email with AttachmentSearch in GridView using JavaScriptHTML to PDF using iTextSharp LibraryImprove ComboBox Performance Using VirtualizingStackPanelExport HTML to ExcelSearch Location in Google Map API and many more. Now we will move on how to disable mouse right click using JavaScript.

 ADD SCRIPT

Above script has to be copied in the head section of the Master Page or ASP.NET (ASPX) page and it can also be placed within the ContentPlaceHolder of the Content Page.
The script disables the following three events in order to disable the Right Click in browsers.

 OnMouseDown

The script first checks whether the document has layers, and if yes then the OnMouseDown event is captured and inside its event handler the event is cancelled by using return false.

 OnMouseUp

And if the document does not have layers then inside the OnMouseUp event handler, first the Mouse Button that was clicked is determined and if the Button is Middle or Right then the event is cancelled using return false.

 OnContextMenu

The OnContextMenu event is cancelled using the OnContextMenu event handler.




2:  Disable text selection in a page



This javascript disables the ability to select text with mouse. You can try it on this page (works on IE and FF).
<SCRIPT type="text/javascript">
if (typeof document.onselectstart!="undefined") {
document.onselectstart=new Function ("return false");
}
else{
document.onmousedown=new Function ("return false");
document.onmouseup=new Function ("return true");
}
</SCRIPT>
However, it's not a good "protection" because you can return to the normal behaviour with a simple javascript url :
... and if Javascript is disabled then the protection is not effective!






Advertisement
advertisement