Tuesday, August 2, 2011

How to prevent Ctr+C in your webpage

This small Javascript snippet will prevent users for performing operations like Ctr+C, Ctr+V and Ctr+N.
Although this is not the secure way and can easily be violated.
This program executes well on Firefox and little change can make it work on Internet Explorer to.


<script type="text/javascript">
 
function MyKeyboardEvents(e) {
    // get key code
    var key_code = (window.event) ? event.keyCode : e.which;
 
    if (e.ctrlKey && (key_code==99 || key_code==118 || key_code==110 || 
                        key_code==67 || key_code==86 || key_code==78)) {
        alert("Operation prohibited");
        e.preventDefault();
    }
}
 
window.document.onkeypress = MyKeyboardEvents;
</script>

No comments:

Post a Comment