Sometimes webpages that have worked hard for a long time, especially some JavaScript special effects, can be easily copied by users who can view the source files. So how can we prevent people from viewing the source code? We can use the event. Button feature. The following table lists the values and meanings of the event. Button attribute: 0 without buttons 1 left click 2. Right-click 3. Press left and right-click 4. Press the intermediate key 5. Press the left and intermediate keys. 6. Press the right and intermediate keys. 7. Press all keys As shown in the preceding table, you can add the following statement between <body> and </body>: <SCRIPT type = "text/JavaScript"> Function click (){ If (event. Button! = 1) {alert ('xxxxxxx ');} Document. onmousedown = click; </SCRIPT> <SCRIPT type = "text/JavaScript"> // The following code disables the right-click operation. You can delete the code that you do not want to disable. <! -- 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) {alert (" Xxxxxxxxxxx ");}} { Event. cancelbubble = true Event. returnvalue = false; Return false; } } Document. oncontextmenu = nocontextmenu; // For ie5 + Document. onmousedown = norightclick; // for all others // --> </SCRIPT> Right-click Disabled Image Code <SCRIPT type = "text/JavaScript"> VaR clickmessage = "right-click the picture on this site to disable it! " Function disableclick (e ){ If (document. All ){ If (event. Button = 2 | event. Button = 3 ){ If (event. srcelement. tagname = "IMG "){ Alert (clickmessage ); Return false; } } } If (document. layers ){ If (E. Which = 2 ){ Alert (clickmessage ); Return false; } } } Function associateimages (){ For (I = 0; I <document. Images. length; I ++) Document. Images [I]. onmousedown = disableclick; } If (document. All) Document. onmousedown = disableclick Else if (document. layers) Associateimages () </SCRIPT> Function: Disable right-click, select, paste, shift, Ctrl, and ALT. <Script language = "JavaScript"> <! -- Function key (){ // Disable shift If (event. shiftkey ){ Window. Close ();} // Disable alt If (event. altkey ){ Window. Close ();} // Disable Ctrl If (event. ctrlkey ){ Window. Close ();} Return false ;} Document. onkeydown = key; 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 ;} } // Right-click Document. oncontextmenu = nocontextmenu; // For ie5 + Document. onmousedown = norightclick; // for all others // --> </SCRIPT> <Body onselectstart = "Return false"; onpaste = "Return false";> How to use JavaScript to prohibit right-clicking, copying, and pasting is often used for websites:
1. <body oncontextmenu = "window. event. returnvalue = false"> the shortcut menu will be completely blocked. <Table border oncontextmenu = return (false)> <TD> NO </table> 2. <body onselectstart = "Return false"> tips for canceling selection and preventing JavaScript Replication 3. onpaste = "Return false": Do not paste 4. oncopy = "Return false;" oncut = "Return false;" prevents duplicate cirpt effects from being copied. |