Prohibit screen capture, copying, and printing for JS applications

Source: Internet
Author: User

The requirement for prohibit screen capture, copying, and printing is required for the project. Copying and printing may easily lead all kinds of online scripts. But how can I disable screen capture? Printscreen is a special key without keycode, so onkeydown becomes useless. However, it would be better to change the way of thinking. We started from the clipboard to adopt a curve-saving strategy. Code As follows:
<Script language = "JavaScript">
Window. setinterval ("clipboardData. setdata ('text','') ", 100 );
</SCRIPT>
The above code clears the clipboard every 100 milliseconds. Script when the page is loaded Program And then start automatic execution. But there is a drawback, no matter whether the webpage is minimized or not, as long as the window is open, all the copy operations on our computer cannot be performed (the script has been clearing the clipboard ), in a sense, the expected results are achieved, but some are not good :(.

We know that all controls have onfocus and onblur events, and the window is no exception. By using these two events, we can only clear the window when it is active. Otherwise, the Operation will stop. The Code is as follows:
<Script language = "JavaScript">
VaR Interval
Window. onfocus = function () {interval = Window. setinterval ("clipboardData. setdata ('text','') ", 100 );}
Window. onblur = function () {window. clearinterval (interval );}
</SCRIPT>
In this way, the problem can be solved perfectly. However, for screenshot programs that do not place screenshots into clipboard, this method is still insufficient.
The latest test practices show that the onfocus and onblur event methods are not satisfactory. When the focus points to other controls on the program page (even the table, window will lose focus and trigger the onblur event to stop executing the clear clipboard command. Do you need to traverse all controls to bind events to their onfocus and onblur? A little confused and disappointed.
In addition, window. onfocus is only the onfocus of the document. If the focus is in the address bar or a menu, the onfocus will also become invalid.
I only use this text to record my thoughts on JS for half a day.

To disable printing, you only need to put the following style code into the program (the printed page content will be blank ):
<Style >@media print {body {display: None }}</style>

Prohibit copy, select, right-click menu:
<Script language = JavaScript>
Function click (){
Return false ;}
Function click1 () {If (event. Button = 2) {return false ;}}
Function ctrlkeydown (){
If (event. keycode = 67 & event. ctrlkey)
{
ClipboardData. setdata ('text ','');
Return false;
}
}
Document. onkeydown = ctrlkeydown;
Document. onselectstart = click;
Document. onmousedown = click1;
</SCRIPT>
<NoScript> <IFRAME src = *. html> </iframe> </NoScript>
<Script language = JavaScript>
<! --
If (window. Event)
Document. captureevents (event. mouseup );
Function nocontextmenu (){
Event. cancelbubble = true
Event. returnvalue = false;
Return false;
}
Function norightclick (e ){
If (window. Event ){
If (E. Which = 2 | E. Which = 3)
Return false;
}
Else if (event. Button = 2 | event. Button = 3)
{
Event. cancelbubble = true;
Event. returnvalue = false; return false ;}
}
Document. oncontextmenu = nocontextmenu; // For ie5 +
Document. onmousedown = norightclick; // for all others
// --> </SCRIPT>

The preceding code runs normally in ie6.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.