简体   繁体   中英

JavaScript, Print Screen and copy and paste

I got some code from the internet, below, and used it in a mock exam application I am doing. This is suppose to prevent people from Printing Screen, copying or cutting from the exam page. The code works perfectly well in Internet Explorer but does not work in the other browsers. I need help to make the code below work in the other browsers to avoid cheating at the site during mock exam. Below is the code:

<script type="text/javascript">
function AccessClipboardData() {
    try {
        window.clipboardData.setData('text', "No print data");
    } catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);
    }
}

setInterval("AccessClipboardData()", 300);

document.onkeydown = function (ev) {
    var a;
    ev = window.event;
    if (typeof ev == "undefined") {
        alert("PLEASE DON'T USE KEYBORD");
    }
    a = ev.keyCode;
    alert("PLEASE DON'T USE KEYBORD");
    return false;
}
document.onkeyup = function (ev) {
    var charCode;
    if (typeof ev == "undefined") {
        ev = window.event;
        alert("PLEASE DON'T USE KEYBORD");
    } else {
        alert("PLEASE DON'T USE KEYBORD");
    }
    return false;
}

Please know that it is entirely impossible to prevent users from copying or screencapping your site from javascript, seeing how they could simply disable js or your function in particular as has been mentioned in the comments already.

If you simply want to discourage people as much as possible you can still use your code, however window.clipboardData.setData only works in IE so it is not strange you would get an error message in other browsers, for thos you would have to use execCommand to copy a set message to the clipboard at you set interval

documnet.execCommand(delete, false, null)

to delete the current selection and then

documnet.execCommand(copy, false, null)

to copy the currently selected text(which you just made sure was nothing)

(for more info on execCommand https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand )

this should work in Firefox, Safari and Chrome, I know of no way to do this in Opera, as neither command will work in that browser

Note however that this will keep overwritting your clipboard as long as the site is open in the browser, so even if someone tried to copy something else entirely they would be unable.

I would like to point out that I provide this function only to show you what the problem with your code, as you will never be able to do what you want to completely without getting people to install third party rights management software on their computer.

I find the following code at Stackoverflow here, by iDhavalVaja and it worked fine.

<script type="text/javascript">

$(function () {

    $(this).bind("contextmenu", function (e) {

        e.preventDefault();

    });

});
</script>
<script type="text/JavaScript">
    function killCopy(e) { return false }
    function reEnable() { return true }
    document.onselectstart = new Function("return false");
    if (window.sidebar) {
        document.onmousedown = killCopy;
        document.onclick = reEnable;
    }
</script>

If you just want to get this working in other browsers, maybe use jQuery (something like this):

       $(document).keydown(function (e) {
            alert("PLEASE DON'T USE KEYBORD");
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM