Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable right click on IFRAME

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.. !!

like image 847
Lax Avatar asked Oct 27 '14 07:10

Lax


People also ask

How do I disable right click in PDF reader?

Save, Print, Rightclick are the inbuilt functionality of PDF viewer. You can't disable those.

How do I disable the PDF toolbar button in iframe jquery?

by making #toolbar=0 you will be able to hide the download and print button.


2 Answers

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.

like image 114
jollelj Avatar answered Sep 21 '22 08:09

jollelj


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.

like image 38
Dave Avatar answered Sep 20 '22 08:09

Dave