Reputation: 3787
how do i prevent the user from Copying any text in asp.net page using jQuery?
Upvotes: 5
Views: 3829
Reputation: 3787
well i used lots of codes to make this like :
1-to disable the right click:
<script src="js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(function () {
$(this).bind("contextmenu", function (e) {
e.preventDefault();
alert("Copy is not allowed");
});
});
</script>
2-disable Selection
<script type="text/javascript">
/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
function disableSelection(target) {
if (typeof target.onselectstart != "undefined") //IE route
target.onselectstart = function () { return false }
else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
target.style.MozUserSelect = "none"
else //All other route (ie: Opera)
target.onmousedown = function () { return false }
target.style.cursor = "default"
}
//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"
var alltables = document.getElementsByTagName("table") for (var i = 0; i < alltables.length; i++) disableSelection(alltables[i]) //disable text selection within all tables on the page
3-I added those to the end of the body tag
<script type="text/javascript">
var somediv = document.getElementById("page-wrap")
disableSelection(somediv) //disable text selection within DIV with id="page-wrap"
</script>
<script type="text/javascript">
disableSelection(document.body) //disable text selection on entire body of page
</script>
now all done .....
thanks guys ,that was really very helpful.
Upvotes: 1
Reputation: 3175
You can disable the right click and also bind keyup event on document to detect copy command key combination "Ctl + C" and returning false.
To disable right click:
jQuery(document).bind("contextmenu", function(e) {
e.preventDefault();
});
To detect Ctl + C:
jQuery(document).ready(function()
{
var ctlPressed = false; //Flag to check if pressed the CTL key
var ctl = 17; //Key code for Ctl Key
var c = 67; //Key code for "c" key
jQuery(document).keydown(function(e)
{
if (e.keyCode == ctl)
ctlPressed = true;
}).keyup(function(e)
{
if (e.keyCode == ctl)
ctlPressed = false;
});
jQuery(".your-no-copy-area").keydown(function(e)
{
if (ctlPressed && e.keyCode == c)
return false;
});
});
Upvotes: 9
Reputation: 30272
This is usually frowned upon, but if you have to do in, here is a plugin
Upvotes: 1