Tips, News and Comments on HTML Guard


July 17th, 2009, by Andreas

Ideas for Anti-Right Click Actions

Introduction

To follow up on my last article on how to use JavaScript to block right-clicks in the browser, I will now share some ideas on spicing up the script a bit. The goal is not to just silently disable the context menu, but also to give the user one of the following forms of feedback:

  • Display a message box window with a custom text
  • Display an inline message right under the cursor
  • Redirect the browser to another website
  • Bookmark a page
  • Send an email to a specified address

The presented solutions are more of a proof-of-concept than something I would actually recommend using. Please decide for yourself what to make of the code.

All code samples can be downloaded here as a ZIP file.

Display a message box window with a custom text

To show a message box every time a user right-clicks on your page, just add the following line of code to the original script’s “cancelContextMenu” function:

alert("Please respect our copyright. Thank you!");

A sample page is available here.

Display an inline message right under the cursor

A slicker and less intrusive way to present a message to a user is to display it as a div popup inside your page. Please click here to see what I mean.

Right Click Popup

The popup is displayed right under the mouse cursor’s current position and disappears after one second.

Redirect the browser to another website

Another possible reaction to a right click is to redirect the user’s browser to a different web page – for example, a page which explains what
‘copyright’ means. This can be accomplished with the following line of code:

location.href = "http://www.wipo.int/copyright/en/general/about_copyright.html";

Bookmark a page

When adding the following code to the original script, the user will be prompted to add your website to the Favorites list everytime he/she tries to open the context menu:

function bookmarkSite(url, title){
  if (isIE5)
    window.external.AddFavorite(url, title);
  else if (isMoz)
    window.sidebar.addPanel(title, url, "");
}
 
bookmarkSite("http://www.website.com/", "Website Title");

An example of the code can be found here.

Send an email to a specified address

Although not really advisable, you can also make the browser open the user’s email client every time a right-click is detected. To do so, just add this code to the original anti-right click script:

location.href = "mailto:test@test.com?subject=Subject goes here&body=Body goes here";

Please click here to see a sample page.