Need help on how to send a variable value from an Iframe back to its parent?

P

paul

HI! How do we send a variable from an Iframe page back to its parent?

I have a script that calculates the iframe's window size but I need to know
how to send that value back to its parent so I can use it there.

Thanks in advance :)

Paul
 
D

Danny

All frames parent is actually Window object, not Document, so, just
do window.myVar=window.MYIFRAME.myVarInTheIframeHere or so, just
address the window level objects, as they're siblings of all iframes.


Danny
 
P

paul

HI! I am unable to get the it to write to page so I can see it. is there
something wrong with code below?

<script language="JavaScript">
<!--
var varborderfromiframe=1;
window.varborderfromiframe=window.Iframemain.y;
document.write(varborderfromiframe)
//-->
</script>

Paul
 
R

RobG

paul wrote:

Please don't top-post. Reply immediately below a trimmed quote of
whatever it is that you are replying to.

If you are using Outlook, you may be able to change the default
behaviour to open replies with the insertion point below the quoted text.

HI! I am unable to get the it to write to page so I can see it. is there
something wrong with code below?

<script language="JavaScript">

The language attribute is deprecated, type is required:


HTML comment delimiters inside script elements serve no useful purpose
and are potentially harmful, just don't use them.

var varborderfromiframe=1;

Here you create a global variable varborderfromiframe with a value of 1.

window.varborderfromiframe=window.Iframemain.y;

Then you immediately replace the value with a reference to the 'y'
property of the object referenced by window.Iframemain. Why not:

var varborderfromiframe = window.Iframemain.y;


If window.Iframemain is not an object or doesn't exist, you will get a
script error. If it does exist but doesn't have a y property, you'll
get 'undefined'.

A more robust approach might be:

var varborderfromiframe = (window.Iframemain && window.Iframemain.y);


I'll guess that window.Iframemain is an attempt to reference an iFrame
whose ID is 'Iframemain'. To get a reference to it from the parent
window, use getElementById:

var o;
if ( document.getElementById
&& (o = document.getElementById('Iframemain'))){

// o is now a reference to the iFrame

}

However, you may have difficulty getting at the attributes of objects
inside the iFrame depending on the domain of document used for the content.

document.write(varborderfromiframe)

This infers that your script is in the parent window, rather than the
iFrame. Your original question was how to pass the value from the
iFrame to the parent window.

You can access the window in which an iFrame is hosted by accessing the
parent of the document's own window object:

var frameParent = window.parent;

If your frame is not hosted in another page, then window.parent will
return the current window, so:

var frameParent = window.parent;
if (frameParent == window){
// frameParent is the current window
// iFrame is not hosted in another window
}


Try this example:

// HTML in z1.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<title>iFrame example</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<script type="text/javascript">

function showValue(x)
{
alert(x);
}

function getIframeVar()
{
var o;
if ( document.getElementById
&& (o = document.getElementById('Iframemain'))){
alert('Iframemain is a ' + o.nodeName
+ '\npassToParent is a : '
+ (o.contentWindow && typeof o.contentWindow.passToParent));
}
}

</script>
</head>
<body>
<p>
<input type="button" value="Get a variable from the iFrame"
onclick="getIframeVar();">
<br>
</p>
<iframe src="z2.html" id="Iframemain" name="Iframemain"></iframe>
<div id="msg"></div>
</body>
</html>


// HTML for z2.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>iFrame content</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<script type="text/javascript">

// Calls the function showValue in the parent object
// and passes the value of 't' to it
function passToParent(t)
{
var x;
if ( (x = window.parent)
&& (x = x.showValue)
&& ('function' == typeof x || 'object' == typeof x) ){
x(t);
}
}
</script>
</head>
<body>
<form action="">
<input type="text" name="testInput" value="Text from iFrame">
<input type="button" value="Pass text to parent"
onclick="passToParent(this.form.testInput.value);">
</form>
</body>
</html>
 
P

paul

Thanks for the info, ps what is top quoting, and what is the problem with
the way that I answer this post?

Paul
 
R

RobG

paul said:
Thanks for the info, ps what is top quoting, and what is the problem with
the way that I answer this post?

In this group (and in most news groups) is it usual to reply immediately
below a quote of the text that one is replying to (read a few posts to
see how it's done). Quotes should be trimmed to just the relevant bits.

You appear to be using Outlook, which I think has the default of putting
the insertion point at the top of the message, above the quoted text. I
think the intention is to let you scan down the message, trimming and
replying as you go rather than to just start typing at the top.


Signatures should have "-- " (dash dash space newline) on the line
immediately above them so that they are not automatically quoted when
replying.

paul wrote: [...]

HTML comment delimiters inside script elements serve no useful purpose and
are potentially harmful, just don't use them.

Some people question this comment, so I'll explain.

The use of <!-- //--> within script elements is ignored by browsers
provided that the opening <!-- tag is before any script content. The
closing tag can be anywhere since it is 'hidden' using script comment
markers.

It is useless because the only browsers that do not understand script
elements are those from before Netscape 2 or IE 3. Even those browsers
should not display any element content that is in the head. Any browser
or user agent after that (i.e. compliant with HTML 3.2 or later) knows
what a script element is and not to display the content, even though
they may not be able to execute the script or even have a script
interpreter.

It is potentially harmful because:

1. Some think <!-- can be used as an opening comment tag anywhere within
the script and don't understand why their script doesn't work when
they do. Some browsers will tolerate it, others wont.

2. Some transfer the delimiters to external script files and can't
understand why their script stops working.

3. Some transfer the script to XHTML where <!-- can potentially hide the
entire script from the browser.

There have been a couple of posts for each of 1 and 2 in the last 12
months. 3 is less of an issue because browsers seem to deal with them
in XHTML the same why they do in HTML - the browser seems to strip them
out before passing the content to the script engine (issues 1 and 2
apply equally to XHTML as HTML).

But that behaviour is non-standard can't be expected to be supported
forever, especially as there will shortly be a vast array of mobile
devices introducing a greatly expanded range of user agents on the web.

So the bottom line is that <!-- //--> is tolerated by modern browsers
when used 'properly', but it does cause problems. It is possible that
future browsers may not tolerate it as there is no requirement in any
public specification for them to do so.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top