Click here to Skip to main content

Javascript hack to disable Right Click and Text Selection

Sometimes we have requirement to disable Right Click context menu and Text selection of a web page from the user, so that the user cannot use the right click default context menu and also to disable text selection so that sensitive data could not be copied from the website. To Disable Context Menudo
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Sometimes we have requirement to disable Right Click context menu and Text selection of a web page from the user, so that the user cannot use the right click default context menu and also to disable text selection so that sensitive data could not be copied from the website.

To Disable Context Menu

document.oncontextmenu = function(){return false;};

If you just place this line in your page under script tag, the context menu will get disabled.

To Disable Text Selection

document.onselectstart= function() {return false;}; 

This will disable Selection in IE. For Mozilla we need to apply CSS to the body element.
-moz-user-select: none;This will disable selection in Mozilla.

You can also disable selection for a single container. For instance :

<div onselectstart="return false;" 
style="-moz-user-select: none;">

 This will disable selection of Text for the container.

Hope this will help you.

Posted 22 Sep '09


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. SAKryukov (1,140)
  2. OriginalGriff (1,065)
  3. Abhinav S (460)
  4. thatraja (455)
  1. SAKryukov (9,034)
  2. Christian Graus (5,771)
  3. OriginalGriff (4,388)
  4. Abhinav S (4,280)
  5. thatraja (4,230)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOne tip to add... PinmemberMatt Gullett13:29 14 Feb '10  
GeneralRe: One tip to add... PinmvpAbhishek Sur23:09 14 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 22 Sep 2009
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid