1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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