473,389 Members | 1,303 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,389 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 9345


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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.