3

Is it possible to disable right click on an iframe? I know it might be possible if the file in the iframe resides in the same domain,but i was wondering if it could be done if the file in the frame was from an external site?

thanks

manraj82
  • 4,809
  • 23
  • 53
  • 83
  • 2
    Sites that disable context menus always make me think of those amateurishly scripted sites that are trying to hide their precious source! – Andy E Feb 16 '10 at 16:25
  • @Andy:Im not trying to hide my precious script here,just trying to prevent the users from refreshing it...i know people still could find a way to refresh it,but was just looking what options i had...cheers nyway – manraj82 Feb 16 '10 at 16:30

5 Answers5

10

You can't really disable the context menu to begin with. You can only construct fragile barricades to keep people from invoking it. But the fact that this is an external iframe only compounds the issue. No, you can't keep the users from activating the context menu on your iframe. Sorry.

Community
  • 1
  • 1
Sampson
  • 251,934
  • 70
  • 517
  • 549
4

works on IE to disable right click on Iframe but the problem is it does not work with external websites ,,, iframed file must be at the same domain ... take a look on it

<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" onload="disableContextMenu();" onMyLoad="disableContextMenu();"></iframe>
</body>
</html>
Dedrick
  • 187
  • 1
  • 11
0

No, it's not possible if it's on an external domain. A mouse click or any other event starts at the first, topmost element it fires on and then works its way back up the chain of elements (unless propagation is stopped). If you tried to stop it at the containing document it will have already fired on the relevant elements of the child document.

Andy E
  • 311,406
  • 78
  • 462
  • 440
0

yes, it is possible to do all following things: disable download, print, save, printscreen, and any button from keyboard to provide security for PDF.

follow my project.....

1 . Install server to run php files ( else use usb portable server ) 2. Create "Pdf_Files" folder in your project and paste your PDF files in it. 3.download pdf.js project 4. write the following codes...

blocked.js

$(function() //right click disabled
{
    $(this).bind('contextmenu',function()
    {
        alert("Function disabled");
        return false;
    })
});

function copyToClipboard() {
  var aux = document.createElement("input");
  aux.setAttribute("value", "Function Disabled.....");
  document.body.appendChild(aux);
  aux.select();
  document.execCommand("copy");
  document.body.removeChild(aux);
  alert("Print screen disabled.");
}

function blockPrint() {
  alert("Print is not allowed...");
}

 function PreSaveAction() { 
    alert("saving..");
 }

$(function()
{
    $(this).keyup(function(e){
      if(e.keyCode == 44 || e.keyCode == 137 ||e.KeyCode == 93 )
      //100 Save 137 SHift F10 93 RightClick 44 PrintScreen
      {
        copyToClipboard();
        return false;
      }
    })
}); 

//disable Ctrl + keys
document.onkeydown = function (e) {
    e = e || window.event;//Get event
    if (e.ctrlKey) {
        var c = e.which || e.keyCode;//Get key code
        switch (c) {
            case 83://Block Ctrl+S
            case 80 : //block Ctrl+P
            case 17 : //block Ctrl
            case 16 : //block Shift
                e.preventDefault();     
                e.stopPropagation();
                alert("key disabled");
            break;
        }
    }
};


$(window).focus(function() {
  $("body").show();
}).blur(function() {
  $("body").show();
});

function setClipBoardData(){ //override system function - make clipBoard null always
    setInterval("window.clipboardData.setData('text','')",20);
}
function blockError(){
    window.location.reload(true);
    return true;
} 

MyIframe.php

<html>
<head>
    <title> </title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="blocked.js"></script>
    <link rel="stylesheet" type="text/css" href="myStyle.css">
</head>

<body onbeforeprint="copyToClipboard()" >
<?php

    $file = './Pdf_Files/';

    if( isset($_REQUEST["path"] ) )
        $file .= $_REQUEST["path"];

    echo ' <iframe src="pdfjs/web/viewer.html?file=../../'. $file .'#toolbar=0&navpanes=0"  /> ';

?>

</body>
</html>

myStyle.css

@media print { * {  display: none; } } /* make print blank */

iframe {
    height: 100%;
    width:100%;
    padding:0;
    overflow-x: scroll;
    border:none;
    overflow-y: scroll;
}

/* disable selection highlighting, from https://stackoverflow.com/a/4407335 */
* {
    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
     -khtml-user-select: none; /* Konqueror HTML */
       -moz-user-select: none; /* Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome and Opera */
}

input[type="submit"] {  /* make submit btn as link */
    background:none!important;
    color:inherit;
    border:none; 
    padding:0!important;
    font: inherit;
    border-bottom:1px solid #444; 
    cursor: pointer;
}

test.php

<html>
<head>
<title>  </title>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="blocked.js"></script>
<link rel="stylesheet" type="text/css" href="myStyle.css">

<body>
    <form method="post" action="MyIframe.php" >

    <table align="center" width="800px" cellspacing="20px" >
<?php

    $path = './Pdf_Files/';
    $count = 0;

    if( $handler = opendir( $path ) )   
    {
        while( false !== ($file = readdir($handler)))
        {
            if( strpos($file, '.pdf' ) !== false )          
            {
                if( $count++ % 2 == 0 ) //make cloumn count 2
                    echo '<tr>';

                echo '<td> * <input type="submit" name="path" value="'. $file .'" /> </td> ';
            }
        }
        closedir($handler);
    }

?>

    </table>
</form>

</body>
</html>

using this full project you can gives any kind of security for your PDF file even you can block any key keyboard for protection purpose.

steps to perform

  1. Start server
  2. Start Web-Browser & enter Url -" localhost:"port"/test.php "
  3. Add any pdf file in this folder " Pdf_Files " folder
  4. Refresh browser
  5. Added files autometicaly fetch on browser window with name .
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
-1

It's possible, if you create a div, and into this Div you have to add z-index.

After configuring width, height, add the filter:alpha(opacity=50); opacity:0.5; so, after that, you put a conde into your site blocking the right click...

msturdy
  • 9,343
  • 8
  • 38
  • 52