Tips, News and Comments on HTML Guard


Posts Tagged ‘HTML’

August 12th, 2009, by Andreas

Disabling the Context Menu in Firefox

Firefox offers an easy way to circumvent the anti-right click script that I presented to you in a previous article. All one has to do is go to the “Advanced JavaScript Settings” (Tools > Options > Content > Advanced) and deselect the “Disable or replace context menus” option.

Firefox - Disable or replace context menus

However, there is no need to panic. There is a simple solution to this problem. By stopping the propagation of the “oncontextmenu” event with the .stopPropagation() method, the context menu can be blocked even with “Disable or replace context menus” remaining unchecked:

function onContextMenu(e) {
  if (e && e.stopPropagation)
    e.stopPropagation();
 
document.oncontextmenu = onContextMenu;

When merging the above code into the original anti-right click script, we get the following:

<script language="JavaScript" type="text/javascript">
<!--
 
  // Setting "onlyOnImages" to "true" will disable right-clicking only for
  // images
  var onlyOnImages = false;
 
  // Detect the browser we have to handle with
  var isIE5 = document.all && document.getElementById;  // IE 5 or higher
  var isMoz = !isIE5 && document.getElementById;  // Mozilla/Firefox
 
  function cancelContextMenu(e) {
    // Here you can add additional code that is executed when the context menu
    // is blocked. For instance you can use the following code to display a
    // message to the user:
    //alert("Please respect our copyright. Thank you!");
 
    if (e && e.stopPropagation)
      e.stopPropagation();
 
    return false;
  }
 
  /* This function is fired every time a user clicks the right mouse button to
     open the browser's context menu. */
  function onContextMenu(e) {
    // Depending on the "onlyOnImages" variable the context menu is either
    // blocked for the complete page or only for <img> tags.
    if (!onlyOnImages
      || (isIE5 && event.srcElement.tagName == "IMG")
      || (IsMoz && e.target.tagName == "IMG")) {
      return cancelContextMenu(e);
    }
  }
 
  if (document.getElementById) {
    // Register event handler
    document.oncontextmenu = onContextMenu;
  }
 
-->
</script>

A sample page is available here.


June 15th, 2009, by Andreas

Disable Right-Clicks on your Website

About Anti-Right-Click Protection

Disabling the browser’s context menu is a classic method to prevent the theft of web page content. With the help of JavaScript, it’s quite easy to block right-clicks by cancelling “oncontextmenu” events (see the code below). Although there are many ways to bypass this kind of protection, and although the solution doesn’t work in all browsers, implementing the solution is often better than doing nothing to prevent content theft.

The Script

To disable right-clicking on your web page, simply copy and paste the following code to the <head> (or <body>) section of your pages’ HTML code:

<script language="JavaScript" type="text/javascript">
<!--
 
  // Setting "onlyOnImages" to "true" will disable right-clicking only for
  // images
  var onlyOnImages = false;
 
  // Detect the browser
  var isIE5 = document.all && document.getElementById;  // IE 5 or higher
  var isMoz = !isIE5 && document.getElementById;  // Mozilla/Firefox
 
  function cancelContextMenu(e) {
    // Here you can add additional code that is executed when the context menu
    // is blocked. For instance, you can use the following code to display a
    // message to the user:
    // alert("Right-click disabled!");
 
    return false;
  }
 
  /* This function is fired every time a user clicks the right mouse button to
     open the browser's context menu. */
  function onContextMenu(e) {
    // Depending on the "onlyOnImages" variable the context menu is either
    // blocked for the complete page or only for <img> tags.
    if (!onlyOnImages
      || (isIE5 && event.srcElement.tagName == "IMG")
      || (IsMoz && e.target.tagName == "IMG")) {
      return cancelContextMenu(e);
    }
  }
 
  if (document.getElementById) {
    // Register event handler
    document.oncontextmenu = onContextMenu;
  }
 
-->
</script>

The script allows you to choose whether or not to disable right-clicks only on images (onlyOnImages = true) or for the complete page (onlyOnImages = true).

Script Demo

Click here to see a demo page that uses the “Anti-Right-Click” script listed above.

Right Click Protection with HTML Guard

By the way, a very easy way to add right-click protection to your web pages is to use HTML Guard, our software solution for preventing websites from being copied. HTML Guard not only allows you to disable the browser’s context menus but also provides protection features such as encrypting HTML source code, preventing text selection or blocking the print function. Please click here to download a fully functional demo version.