Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Disallow Selection in web-page using JavaScript (http://www.go4expert.com/articles/disallow-selection-web-page-using-t2177/)

pradeep 9Dec2006 16:07

Disallow Selection in web-page using JavaScript
 
Sometimes, we do not want people to copy the text on some of our web pages, here is a script which prevents user (not advanced users) from selecting the text.
For better results put the JavaScript code inline and not in a separate JS file.

Code: JavaScript

// specify allowed tags for some browsers
var allowedTags=["input", "textarea", "select"]

allowedTags=allowedTags.join("|");

function disableselect(e)
{
    if (allowedTags.indexOf(e.target.tagName.toLowerCase()) == -1)
        return false;
}

function reEnable()
{
    return true;
}

if (typeof document.onselectstart != "undefined")
    document.onselectstart=new Function ("return false");
else
{
    document.onmousedown=disableselect;
    document.onmouseup=reEnable;
}



All times are GMT +5.5. The time now is 22:00.