Tuesday, July 26, 2011

How to prevent someone copy information from your website

The first thing that I want to do when I start my blog is how to prevent someone else copy information from my blog. I did some search on the Internet before I dig in to it. Then I realized there are two things we should do to prevent it happen: disable showing context-menu and disable mouse right click and ctrl + c.

First is to disable showing context-menu. The easiest way is to capture the onContextMenu even, and return false in its event handler. You can add this code in the body of your html file.

<body oncontextmenu='return false;'>

or if you can’t reach the body tag you can add a JavaScript in any place of your html file like this:

 

<script language="JavaScript">
function onContextMenu()
{
   event.returnValue = false;
}
document.oncontextmenu=onContextMenu
</script>

Second is to disable mouse right click and ctrl + c. You can add a JavaScript in any place of your html file like this:

<script language="JavaScript">
function click()
{
  if((event.button==2)||(event.button==3))
  {
   // disable mouse right click
   return false;
  }
  return false;
} // click
document.onmousedown=click
 
function onKeyDown()
{
  // current pressed key
  var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
  if (event.ctrlKey && (pressedKey == "c" ||
                        pressedKey == "v"))
  {
    // disable key press porcessing
    event.returnValue = false;
  }
} // onKeyDown
document.onkeydown=onKeyDown
</script>

No comments:

Post a Comment