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

September 2nd, 2004, 12:55 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 2nd, 2004, 09:06 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

September 3rd, 2004, 01:44 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 3rd, 2004, 03:44 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

September 3rd, 2004, 11:48 PM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 27th, 2006, 08:01 PM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |