How to disable right click on IFRAME

前端 未结 6 2151
花落未央
花落未央 2020-12-11 18:14

I have a document content displayed on IFrame in MVC web application. The content should not be copied and printed . I tried to disable right click using two functions style

相关标签:
6条回答
  • 2020-12-11 18:21

    Your question is a little confusing as the title is about right clicking, yet the bddy of the question is about copying and pasting and about using the print screen button. Whilst you can do some things with the right click button (already answered by other posts and well documented) generally your question is how to prevent people accessing the code/content or taking a print out of your content.

    This isn't possible. Whilst you can make it more tricky for some users, it will never succeed against those who are determined enough.

    First of, even if you (somehow) disabled the print screen button on the keyboard, there are many screen capture programs out there... And I can't see how it will (ever) be possible to detect another program doing this from within the limitations of website code.

    Any javascript solution can fail, they can turn off javascript.

    Even if you managed to prevent some one from viewing the source code and copying the HTML, some one could just scrape the content direct from the site.

    I have a friend who is a graphic designer and he wanted to do this (disable people copying images in this case). I told him not to bother, if they want to take the content you put into the public domain, they will. A water mark may help but only in some situations. Personally, I'd give up on this task and just accept it, and focus on more interesting tasks.

    0 讨论(0)
  • 2020-12-11 18:22

    We can't just disable right click on the iframe. Because of the iframe content is loading from another source so our code will not work on it. Here I found a solution which is the only option we have.

    <html>
    <head>
    <title>Disable Context Menu</title>
    <script type="text/jscript">
      function disableContextMenu()
      {
        window.frames["fraDisabled"].document.oncontextmenu = function(){alert("No way!"); return false;};   
        // Or use this
        // document.getElementById("fraDisabled").contentWindow.document.oncontextmenu = function(){alert("No way!"); return false;};;    
      }  
    </script>
    </head>
    <body bgcolor="#FFFFFF" onload="disableContextMenu();" oncontextmenu="return false">
    <iframe id="fraDisabled" width="528" height="473" src="local_file.html"></iframe>
    <div style="width:528px;height:473px;background-color:transparent;position:absolute;top:0px;">
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-11 18:23

    In order to disable the right click menu you could use the following snippet:

    document.oncontextmenu = function() { 
        return false; 
    };
    

    I made a JSFiddle that displays the effect.

    0 讨论(0)
  • 2020-12-11 18:24

    This worked for me fine:

    window.frames["your_iframe_id"].contentDocument.oncontextmenu = function(){
     return false; 
    };
    
    0 讨论(0)
  • 2020-12-11 18:24

    1.) Disabling a right-click in iFrame using jquery

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    // Function to block the right click in the iFrame
    <script type="text/jscript">
    function injectJS(){    
        var frame =  $('iframe');
        var contents =  frame.contents();
        var body = contents.find('body').attr("oncontextmenu", "return false");
        var body = contents.find('body').append('<div>New Div</div>');    
    }
    </script>
    

    Call the "injectJS()" function in the iFrame

    <iframe id="myiframe" width="528" height="473"  onload="injectJS()"></iframe>
    

    2.) Disable the right-click in the webpage

    With javascript alone

    document.addEventListener('contextmenu', event => event.preventDefault());
    

    Here's an example in jQuery (Note: pressing the right mouse button will fire three events: the mousedown event, the contextmenu event, and the mouseup event)

    // With jQuery
    $(document).on({
    "contextmenu": function(e) {
        console.log("ctx menu button:", e.which); 
        // Stop the context menu
        e.preventDefault();
    },
    "mousedown": function(e) { 
        console.log("normal mouse down:", e.which); 
    },
    "mouseup": function(e) { 
        console.log("normal mouse up:", e.which); 
    } 
    });
    

    If you have any questions leave a comment below.

    0 讨论(0)
  • 2020-12-11 18:29
    window.frames["your_iframe_id"].document.oncontextmenu = function(){ return false; };
    
    0 讨论(0)
提交回复
热议问题