1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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