JavaScript禁止右鍵及粘貼復(fù)制

字號(hào):


    由于項(xiàng)目要求,有些文本框需要禁用掉右鍵和復(fù)制粘貼的功能,昨天剛剛用JS實(shí)現(xiàn)。
    view sourceprint?
    01 function click(e)
    02 {
    03 if (document.all)
    04 {
    05 if (event.button==1||event.button==2||event.button==3)
    06 {
    07 oncontextmenu='return false';
    08 }
    09 }
    10 if (document.layers)
    11 {
    12 if (e.which == 3)
    13 {
    14 oncontextmenu='return false';
    15 }
    16 }
    17 }
    18
    19 if (document.layers)
    20 {
    21 document.captureEvents(Event.MOUSEDOWN);
    22 }
    23 document.onmousedown=click;
    24 document.oncontextmenu = new Function("return false;")
    25
    26 var trxdyel=true
    27 var hotkey=17 /* hotkey即為熱鍵的鍵值,是ASII碼,這里99代表c鍵 */
    28 if (document.layers)
    29 document.captureEvents(Event.KEYDOWN)
    30 function gogo(e)
    31 {
    32 if (document.layers)
    33 {
    34 if (e.which==hotkey && trxdyel)
    35 {
    36 alert('操作錯(cuò)誤.或許是您按錯(cuò)鍵了!');
    37 }
    38 }
    39 else if (document.all)
    40 {
    41 if (event.keyCode==hotkey&&trxdyel){ alert('操作錯(cuò)誤.或許是您按錯(cuò)鍵了!'); }}
    42 }
    43
    44 document.onkeydown=gogo
    將以上JS代碼寫到JS文件中取名為xp.js并放入Script文件夾中,引用時(shí)需要注意設(shè)置Charset=“utf8”,不然提示出的信息會(huì)是亂碼。頁面引用:view sourceprint?1 <script src="../Script/xp.js" type="text/javascript" charset="utf8"></script>