Click to See Complete Forum and Search --> : changing parent objects from child page


rvl
01-30-2008, 10:46 PM
I have a document with a <div> layer and and iframe.

The <div> is set to be visible and the iframe is set hidden.

Clicking a button in the <div> calls a function that loads a frameset into the iframe then makes the iframe visible and hides the <div>.

I want a button in the frameset to hide its parent iframe and show the <div> again.

The following function works - if the button in the iframe is not contained in a frameset. The function is passed the names of the <div> and iframe.

function toggleDisplay(d1,d2) {
if(parent.document.getElementById(d1).style.display == "none")
{parent.document.getElementById(d1).style.display = "block"; }
else { parent.document.getElementById(d1).style.display = "none"; }
if(parent.document.getElementById(d2).style.display == "none")
{ parent.document.getElementById(d2).style.display = "block"; }
else {parent.document.getElementById(d2).style.display = "none"; }
}

But I'm having no luck getting out of the frameset. I've tried variations like:
parent.parent.document... parent.parent.parent.document... crazy stabs in the dark.

Can someone offer a solution and shed some light on this?

gil davis
01-31-2008, 06:23 AM
Page #1:

<script type="text/javascript">
function toggleDisplay(d1,d2) {
if(parent.document.getElementById(d1).style.display == "none")
{parent.document.getElementById(d1).style.display = "block";}
else
{parent.document.getElementById(d1).style.display = "none";}
if(parent.document.getElementById(d2).style.display == "none")
{parent.document.getElementById(d2).style.display = "block";}
else
{parent.document.getElementById(d2).style.display = "none";}
}
</script>
<div id="d1" style="display: block">
This is some stuff<br>
<input type="button" value="click" onclick="toggleDisplay('d1','d2')">
</div>
<iframe id="d2" src="framesetwithbutton.htm" style="display: none"></iframe>

Page #2, called "framesetwithbutton.htm":

<frameset cols="50%,*">
<frame name="f1" src="pagewithbutton.htm"></frame>
<frame name="f2" src="pagewithbutton.htm"></frame>
</frameset>

Page #3, called "pagewithbutton.htm":

<input type="button" value="click" onclick="window.top.toggleDisplay('d1','d2')">

rvl
01-31-2008, 08:04 PM
When I tried your solution (and my original) I notice an Error on Page in the status bar. The detailed message states that access is denied.

A simple Google search show this problem has something to do with IE7 security feature denying permission when trying to script across frames.

Both your code and my original code works in Firefox.

I don't know yet if there is a workaround.

toenailsin
01-31-2008, 08:11 PM
do you need to use frames?

you culd make another div with the contents of the frame and switch between the 2 divs

rvl
01-31-2008, 08:16 PM
yes, in a way. I'm loading framesets that come ready-made form another source and it would be a lot of work to rebuild them without frames. I guess I will if I have to.

toenailsin
01-31-2008, 08:34 PM
i made this which seems to work fine. tested it in IE6, FF and Opera

Main page:
<html>
<head>
<script><!--

var Div, Frame;
function Init(){
Div = document.getElementById('Div');
Frame = document.getElementById('Frame');
}
window.onload = Init;

function ShowFrame(){
Frame.style.display = 'block';
Div.style.display = 'none';
}

--></script>
<style><!-- #Frame { display: none; } --></style>
</head>
<body>

<div id="Div"><a href="javascript:ShowFrame()">Show Frame</a></div>
<iframe src="blah.php" id="Frame" name="Frame"></iframe>

</body>
</html>

Frame:
<html>
<head>
<script><!--

function HideFrame(){
parent.Frame.style.display = 'none';
parent.Div.style.display = 'block';
}

--></script>
</head>
<body>

<div id="Div"><a href="javascript:HideFrame()">Hide Frame</a></div>

</body>
</html>

rvl
02-01-2008, 07:56 AM
Thanks for looking into this, but I'm still getting the permission denied error.

The problem is an IE7 security feature not allowing scripting between different windows and frames.

http://msdn2.microsoft.com/en-us/library/ms533028.aspx

I'm going to find another way of doing what I need to do.