Disallow Selection in web-page using JavaScript

Discussion in 'JavaScript and AJAX' started by pradeep, Dec 9, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:
    // 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;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice