Click here to Skip to main content
6,917,296 members and growing! (14,100 online)
Email Password   helpLost your password?

Javascript hack to disable Right Click and Text Selection

0
answers
4.9/5
4 votes
746
views

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 1:41 PM
Abhishek Sur 23,270
Editor 132
Enquirer 4
|

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

You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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


Last Updated 26 Mar 2009
Web19 | 2.2.0254 | Advertise | Privacy
Copyright © CodeProject, 1999-2010
All Rights Reserved. Terms of Use