javascript 在 Mozilla Firefox 中禁用对 HTML 页面内容的选择、复制和粘贴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8352367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:10:18  来源:igfitidea点击:

Disable select, copy and paste of the content of HTML pages in Mozilla Firefox

javascriptjquery

提问by Pappu

I want to disable select, copy and paste of the content of HTML pages in Mozilla Firefox. I have used jQuery and JavaScript to disable right-click and copy, select, paste of the contents of HTML pages and it's working fine in IE and Chrome, but not working properly in Mozilla Firefox.

我想在 Mozilla Firefox 中禁用对 HTML 页面内容的选择、复制和粘贴。我已经使用 jQuery 和 JavaScript 禁用右键单击和复制、选择、粘贴 HTML 页面的内容,它在 IE 和 Chrome 中工作正常,但在 Mozilla Firefox 中无法正常工作。

Can we disable copy, paste option in Mozilla Firefox? Any suggestions?

我们可以在 Mozilla Firefox 中禁用复制、粘贴选项吗?有什么建议?

回答by Manish Ojha

Just copy and Paste the below javascript in your webpage:

只需将以下 javascript 复制并粘贴到您的网页中:

<script language="javascript" type="text/javascript"> 
      function disableselect(e) {             
          return false 
      } 
      function reEnable() { 
          return true 
      } 

      document.onselectstart = new Function("return false") 

      if (window.sidebar) { 
          document.onmousedown = disableselect                    // for mozilla           
          document.onclick = reEnable 
      } 

      function clickIE() { 
          if (document.all) { 
              (message); 
              return false; 
          } 
      } 


      document.oncontextmenu = new Function("return false") 

      var element = document.getElementById('tbl'); 

      element.onmousedown = function () { return false; }        // For Mozilla Browser

   </script>

Note:If the above code not works for Firefox then add style="-moz-user-select:none" in the body tag which needs to be restricted alongwith the above code.

注意:如果上述代码不适用于 Firefox,则在需要限制的 body 标签中添加 style="-moz-user-select:none" 和上述代码。

回答by Avanish Kumar

Try this first work for block select and second allow to select in textarea and input field.

尝试第一个块选择工作,第二个允许在文本区域和输入字段中选择。

html,body{
        -webkit-user-select: none;
        -moz-user-select: -moz-none;
        -ms-user-select: none;
        user-select: none;
        -khtml-user-select: none;
    }
    input, textarea{
        -webkit-user-select:  text !important;
        -moz-user-select:  text !important;
        -ms-user-select: text !important;
        user-select: text !important;
        -khtml-user-select: text !important;
    }

回答by Zealotry

A) Apply this code in your html document inside the body tag

A) 将此代码应用到您的 html 文档中的 body 标签内

ondragstart='return false' onselectstart='return false'– to disable copy and paste

ondragstart='return false' onselectstart='return false'– 禁用复制和粘贴

B) Use this script in your html document before closing the closing the head tag

B) 在关闭 head 标签之前在您的 html 文档中使用此脚本

<script language=JavaScript>
    var message=”Function Disabled! OR whatever text you want to show on right click”;         
    function clickIE4(){
        if (event.button==2){ alert(message); return false; }
    }
    function clickNS4(e){
        if (document.layers||document.getElementById&&!document.all){ 
            if (e.which==2||e.which==3){ 
                alert(message);
                return false; 
            } 
        }
    }
    if (document.layers){ 
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown=clickNS4;
    } else if (document.all&&!document.getElementById){
        document.onmousedown=clickIE4; 
    } 
    document.oncontextmenu=new Function(“alert(message); return false”) 
</script>

C) Give security to the contents by using the css elements in body tag

C) 通过在 body 标签中使用 css 元素为内容提供安全性

EX: in css file

例如:在 css 文件中

body {
    user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    -o-user-select: none;
}

This is BASIC protection! Advanced users will bypass it with ease by disabling JavaScript on their browser or using firebug.

这是基本的保护!高级用户可以通过在浏览器上禁用 JavaScript 或使用 firebug 轻松绕过它。