473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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

Jan 7 '06 #1
6 9346


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
Jan 7 '06 #2
HI! thanks, thats what I needed to know :)

Paul
"Danny" <da*******@bluebottle.com> wrote in message
news:%g******************@newssvr29.news.prodigy.n et...


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

Jan 7 '06 #3
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
"paul" <pa***********@sympatico.ca> wrote in message
news:Vt*********************@news20.bellglobal.com ...
HI! thanks, thats what I needed to know :)

Paul
"Danny" <da*******@bluebottle.com> wrote in message
news:%g******************@newssvr29.news.prodigy.n et...


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


Jan 7 '06 #4
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:

<script type="text/javascript">

<!--
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>
--
Rob
Jan 9 '06 #5
Thanks for the info, ps what is top quoting, and what is the problem with
the way that I answer this post?

Paul

"RobG" <rg***@iinet.net.au> wrote in message
news:rA****************@news.optus.net.au...
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:

<script type="text/javascript">

<!--


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

Jan 16 '06 #6
paul wrote:
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.

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


"RobG" <rg***@iinet.net.au> wrote in message
news:rA****************@news.optus.net.au...
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.

[...]
--
Rob
Jan 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lee | last post by:
I am using a modal window and an iFrame to try and pull a return value back. I am doing this across domains. I have the value returned from the modal window to the iFrame window but I can not...
11
by: HolaGoogle | last post by:
Hi, Sorrryy to ask such basic question but i do need your help! Here's what i'm trying to do: In my parent form i'm calling a my Iframe form to get certain value, then depending on that value...
4
by: coolsti | last post by:
I am aware of the setInterval and setTimeout functions for Javascript, but I can't seem to find an example that lets me do what I need to. I have a script that needs to wait until a certain...
3
by: hendedav | last post by:
Hi gang. As with any other post, I am working on a project and have gotten stuck. I am trying to obtain a variable value in the parent webpage from an <iframe>. For instance: Parent Page code...
2
by: Bart Van der Donck | last post by:
Hi, I have a directory with a file main.htm: <html><body> <p><iframe src=i.htm></iframe></p> <p><a href="javascript:history.back()">Back</a></p> </body></html> and a file i.htm:
19
by: k.karthikit | last post by:
Hello all, In some hidden variable (<input type="hidden" name="hiddenId" value="test" /> ,i stored some value.I accessed the value "test" using var id = document.getElementById( 'hiddenId' );...
2
by: Erich93063 | last post by:
I have a page that pops open a window and in that pop up page is an iframe. How can I send information back to the ORIGINAL parent page that popped open the pop up from the iframe page? Hope...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.