Monday, March 29, 2010

Unselectable text script with hack for Chrome

Now I don't know if it is useful 4 U or not, but it'll B an eye catcher 4 sure. Many people R paranoid that what they put on their site will B copied by others. Now U can always add a script 2 block right-click like this one,
function disableRightClick(e){
var message = "Right click disabled";
if(!document.rightClickDisabled) // initialize
document.onmousedown = disableRightClick;}
else document.oncontextmenu = disableRightClick;
return document.rightClickDisabled = true;}
if(document.layers || (document.getElementById && !document.all)){
if (e.which==2||e.which==3){
return false;}}
return false;}}

Now, this takes care of right-clicks, but if U want 2 go ahead with this then try this. Copy & paste this code in a .js file, this will stop users from selecting any text or image from your web page.
Links will not B affected. Here is D code,
var Unselectable = {
enable : function(e) {
var e = e ? e : window.event;
if (e.button !=1) {
if ( {
var targer =;
} else if (e.srcElement) {
var targer = e.srcElement;
var targetTag = targer.tagName.toLowerCase();
if ((targetTag != "input") && (targetTag != "textarea")){
return false;
disable : function () {
return true;
if (typeof(document.onselectstart) != "undefined") {
document.onselectstart = Unselectable.enable;
} else {document.onmousedown = Unselectable.enable;
document.onmouseup = Unselectable.disable;}
Done now link it with any page & it'll B unselectable.
P.S.- Don't know Y but it doesn't works in Chrome. So I found a hack that works in Chrome.
Just add this code D body tag,
<body ondragstart="return false" onselectstart="return false">
Hope it helps U.

No comments:

Post a Comment