Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 2nd, 2004, 12:55 AM
Authorized User
 
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sherbir
Default IFrame and the contextmenu

Hello All,

Here's my problem...I have designed a simple editor in JSP using IFrame.

In the Iframe, I am calling a local file.

My problem is that I want that the user should not be able to right-click in the IFrame.

What's the Javascript solution to this ?

My code is given below :

<%@ page language="java"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> edittest.jsp </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<script language="javascript">
document.oncontextmenu=function (){alert("SORRY !! No right-click here.");return false;};

var isHTMLMode=false;
var iViewContentLoaded = false;
var mywin;

function addContent() {
  if(!iViewContentLoaded) {
      if(iView.document.body) {
      var contents = document.f1.textname.value;
      iView.document.body.innerHTML = contents;
      // iView.document.designMode='On';
      iViewContentLoaded = true;
                               }
                            }
                        }
function Init()
{
    iView.document.designMode='On';
    setTimeout("Init2()",1000);
}

function Init2()
{
      var show_data;
      show_data=document.all.unformatted_text.value;
      // alert(show_data);
      document.all("iView").src=show_data;
      iText=getDocument().body.innerText();
      iText.document.oncontextmenu=function() {return false;};
      getDocument.body.innerHTML=iText;
}
</script>

<BODY onLoad="Init()" bgcolor="#ffffff">
<center>


<table id="tblCtrls" width="700px" height="30px" cellspacing="0" cellpadding="0" bgcolor="#efedde" border="1">
    <tr align="left">
        <td class="tdClass" align="center">
            <b>[u]Resume-Editor</u></b>
        </td>
    </tr>
<table>

<table id="tblCtrls" width="700px" height="30px" cellspacing="0" cellpadding="0" bgcolor="#efedde" border="1">
    <tr>
        <td class="tdClass" align="center">
            <a href="javascript:mode()"><b>Edit</b></a>
        </td>
        </tr>
</table>

<iframe id="iView" style="width: 700px ; height: 345px" border="10" oncontextmenu="return false;">
</iframe>


<input type=hidden name=unformatted_text value="c:\\demo\\resume_file.html">

</center>
</BODY>
</HTML>

How am I supposed to stop the right-click in the editor ? The idea works if I right-click in the main document but doesn't work in the IFrame.

Also, I want to stop any keyboard inputs within the IFrame except the spacebar.

Please help !!


Regards,
Sherbir
__________________
Regards,
Sherbir
 
Old September 2nd, 2004, 09:06 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Here's a simple example, try right clicking before and after pressing the button. Beware, if you have the iframe's document from a different source to your main page then you will be denied access to the document.
Code:
<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">
<input type="button" value="Disable Context Menu" onclick="disableContextMenu();"><br><br>
<iframe id="fraDisabled" width="200" height="200" src="nocontext.htm"></iframe>
</body>
</html>
--

Joe
 
Old September 3rd, 2004, 01:44 AM
Authorized User
 
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sherbir
Default

Quote:
quote:Originally posted by joefawcett
 Here's a simple example, try right clicking before and after pressing the button. Beware, if you have the iframe's document from a different source to your main page then you will be denied access to the document.
Code:
<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">
<input type="button" value="Disable Context Menu" onclick="disableContextMenu();"><br><br>
<iframe id="fraDisabled" width="200" height="200" src="nocontext.htm"></iframe>
</body>
</html>
--

Joe
Hey thanx abunch Joe. Urs was an eye opener !! It works fantastic.

But can u explain to me why I get Access Denied and what's the way to avoid that in case I want to call a different file in the iframe ?

Also I'd like to know how to trap keypress events in the iframe.


Regards,
Sherbir
 
Old September 3rd, 2004, 03:44 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Access is denied if the documents are from different domains, it's a security issue to stop malevolent pages opening your previously viewed pages in an iframe and reading form data.
You can trap keypress by having a handler for onkeydown or onkeypress.

--
Joe
 
Old September 3rd, 2004, 11:48 PM
Authorized User
 
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sherbir
Default

Quote:
quote:Originally posted by joefawcett
 Access is denied if the documents are from different domains, it's a security issue to stop malevolent pages opening your previously viewed pages in an iframe and reading form data.
You can trap keypress by having a handler for onkeydown or onkeypress.

--
Joe
Thanx again Joe. I appreciate ur prompt reply. Thanx agin 4 helping me solve my problems.

Regards,
Sherbir
 
Old April 27th, 2006, 08:01 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I am having a problem with having an IFrame that simply has a jpg as it's source. i'm trying to disable right click, but no scripts seem to apply to the IFrame. Neither does any of the encrypt software out there.

I have this:
<IFRAME src="nbd_6b_s1.jpg" name=image scrolling=no frameBorder=0 width=255 height=360></IFRAME>

and i've tried disabling right click with html in the body tag and the no-right click software, but it doesn't apply to the IFrame. i tried to manipulate the information I saw posted below, but am not that coherent in java script. Any help, direction, ANYTHING would be greatly appreciated.

Thanks,
Liz
 
Old April 28th, 2006, 01:48 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi lizrosenshine!!

Plz check this link

http://p2p.wrox.com/topic.asp?TOPIC_ID=43509

Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
ContextMenu obrienkev Visual Studio 2005 1 November 30th, 2007 03:21 AM
Page 370 - New ContextMenu MajorD BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 2 July 25th, 2007 10:26 AM
values from one iframe to another iframe URGENT raaj Beginning PHP 2 February 27th, 2007 12:19 PM
IFrame and the contextmenu harshad Javascript 1 November 1st, 2006 02:20 AM
IFrame and the contextmenu harshad Javascript 2 October 31st, 2006 02:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.