Cross-browser method to prevent all methods of text copying from a textarea?

牧云@^-^@ 提交于 2019-12-01 17:36:21

问题


I am working on an online typing software. In the typing software, all is going well but I have the problem of dishonest users who might possibly type the text into the textarea, copy it, then reload the page (therefore resetting the timer) and pasting it in straightaway. So I was thinking along the lines of using something like evt.preventDefault(); when javascript detects the pressing of the ctrl / cmd button along with the C key. But then I realized that the user could always go up to the menu bar to press Edit -> Copy. So I was wondering, is there a cross-browser method to disable both methods of copying?


回答1:


You can try to use the following jQuery code:

$('input[type=text],textarea').bind('copy paste cut drag drop', function (e) {
   e.preventDefault();
});



回答2:


You maybe could do something like:

var txtArea = document.getElementById("YourTextAreaId");
txtArea.oncopy = function() { return false; } 
txtArea.onpaste = function() { return false; } 
txtArea.oncut = function() { return false; } 

But even then, the user can copy the content by other means, as suggested in your question.




回答3:


You can block some events, but preventing such user behaviour is not possible. User can always copy text from DOM node via browser console.




回答4:


Bind the event handlers and prevent the clipboard function like such:

$('textarea').on('copy paste cut drag drop', function (e) {
   e.preventDefault();
});


来源:https://stackoverflow.com/questions/10070939/cross-browser-method-to-prevent-all-methods-of-text-copying-from-a-textarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!