SLaks.Blog

Making the world a better place, one line of code at a time

On copy prevention in HTML, part 1

Posted on Sunday, April 08, 2007, at 6:27:00 PM UTC

Many web developers like to prevent their viewers from copying their text. While I do not approve of this, there are cases where it is appropriate.

The simplest way to achieve this is to use the IE only attribute UNSELECTABLE and the FireFox only css style -moz-user-select. Such HTML looks like this:

<DIV unselectable="on"
style="-moz-user-select:none;">
You can't select me.
</DIV>
You can't select me.

To make the HTML and CSS validate, one could do this in Javascript: Elem.unselectable = "on"; Elem.style.MozUserSelect = "none";

However, this method only works in IE and Firefox. In addition, in IE, it doesn't work very well, and if a user tries hard, he will end up selecting the text.

A slightly better way to do it is to handle the onselectstart event (for IE) and the onmousedown event (for everything else) and return false. This will prevent the browser from handling the events. This results in something like this:

<DIV
onselectstart="return false;"
onmousedown="return false;" >
You can't select me.
</DIV>
You can't select me.

The problem with these methods is that they do nothing to prevent a user from reading the HTML source. This is discussed in the next part.

Categories: html, javascript Tweet this post

comments powered by Disqus