8

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="pointer-events:none;" oncontextmenu="return false" for Iframe, which is working fine. But on right click, the pop up with 'View Frame Source', 'View Source' are displaying. How can I restrict this.! Also, how to restrict the print screen option. I know there are other utilities from where anybody can capture data. But the client wants to restrict the print screen option.

<script lang=JavaScript>
    function clickIE() {
        if (document.all) {
            return false;
        }
    }
    function clickNS(e) {
        if (document.layers || (document.getElementById && !document.all)) {
            if (e.which == 2 || e.which == 3) {
                return false;
            }
        }
    }
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown = clickNS;`enter code here`
    }
    else {
        document.onmouseup = clickNS;
        document.oncontextmenu = clickIE;
    }
    document.oncontextmenu = new Function("return false")

<body   oncontextmenu="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false" >
<div  id="div1" style="background-color:Red; height:120px">


  <iframe id="id1" src="" name="I1" scrolling="no" height="100%" width="100%" marginwidth ="0" marginheight="0" onload="disableContextMenu();" style="pointer-events:none;"  /> 



</div>

Please Any help appreciated.. !!

Lax
  • 115
  • 1
  • 1
  • 12
  • 6
    You can't prevent a user to copy something if he can see it. I mean, not at all. – Frederik.L Oct 27 '14 at 07:49
  • Oh, but I was hoping for a roundabout solution :-). Can we restrict the print screen option for iframe content? – Lax Oct 27 '14 at 07:51
  • 2
    i have camera on my phone. lol – jay temp Oct 27 '14 at 07:53
  • If you mean you want to stop them screen shotting it then no you can't. The best you can do is make it super hard my disabling text selection, right clicks and removing the element from the DOM on print (IE only) – MarshallOfSound Oct 27 '14 at 07:53
  • I have never tried it but you may be able to disable some of the key presses required for the print screen shortcut (Ctrl + Shift + PrntScrn) You might be able to e.preventDefault() against the Ctrl and shift keys? But I doubt that would work... Worth a try though :D – MarshallOfSound Oct 27 '14 at 07:54
  • 1
    The only way you can protect against screenshots is to not show the content to the user in the first place. So just abandon the project completely and no screenshots! Yay! ;) – misterManSam Oct 27 '14 at 07:55
  • You could use CSS to hide the iframe when printing. – TenorB Oct 27 '14 at 07:55
  • I donot have much information on removing the element from DOM on print? Is there a code , Also code for disabling right clicks for iframe..Thanx – Lax Oct 27 '14 at 07:57
  • @MarshallOfSound You can disable keys while the document has the focus. Then, clicking outside of the window will allow those keys. One partial, creepy solution would be to hide the document when it doesn't have focus. Even thought it would discourage some grandmas, any script kiddie can disable Javascript and do the above. – Frederik.L Oct 27 '14 at 07:57
  • Same goes for CSS. You can remove any client scripts or protection. – Frederik.L Oct 27 '14 at 07:59
  • I cant hide the iframe, the content is displaying is on iframe.. – Lax Oct 27 '14 at 07:59
  • possible duplicate of [Stop User from using "Print Scrn" / "Printscreen" key of the Keyboard for any Web Page](http://stackoverflow.com/questions/3130983/stop-user-from-using-print-scrn-printscreen-key-of-the-keyboard-for-any-we) – SD7 Oct 27 '14 at 08:00

6 Answers6

8

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.

jollelj
  • 1,010
  • 1
  • 10
  • 18
3

This worked for me fine:

window.frames["your_iframe_id"].contentDocument.oncontextmenu = function(){
 return false; 
};
Amaresh S M
  • 2,936
  • 2
  • 13
  • 25
2

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.

Dave
  • 8,163
  • 11
  • 67
  • 103
  • 2
    IMHO, this question is a "Not a real question" candidate and should be closed, simple as that. I would also add that these behavioral-blocking solutions are more annoying to the user, which will not be tempted to come back, than useful in any way to prevent copy. A dozen of years ago, internet was full of crappy scripts that would say "Do not copy!!!" whenever you wanted to interact with the page. The best thing to do would be to tell the client it cannot be done, and doing it partially can hurt his audience. – Frederik.L Oct 27 '14 at 08:44
1

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>
1

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.

Muthukumar
  • 554
  • 4
  • 9
0
window.frames["your_iframe_id"].document.oncontextmenu = function(){ return false; };
boycod3
  • 5,033
  • 11
  • 58
  • 87
  • I tried with the same line of code, but its not working.the copy ,print options are not displaying now. But 'View Source' and 'View Frame Source','Save As' options are still visible. Is there any way to restrict this.. Thanks – Lax Oct 27 '14 at 08:18
  • The client has requested this functionality, that is why I raised this question. Anyways, Thank you all for taking time to respond – Lax Oct 27 '14 at 08:50