473,722 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9387


All frames parent is actually Window object, not Document, so, just
do window.myVar=wi ndow.MYIFRAME.m yVarInTheIframe Here 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*******@blue bottle.com> wrote in message
news:%g******** **********@news svr29.news.prod igy.net...


All frames parent is actually Window object, not Document, so, just
do window.myVar=wi ndow.MYIFRAME.m yVarInTheIframe Here 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="JavaS cript">
<!--
var varborderfromif rame=1;
window.varborde rfromiframe=win dow.Iframemain. y;
document.write( varborderfromif rame)
//-->
</script>

Paul
"paul" <pa***********@ sympatico.ca> wrote in message
news:Vt******** *************@n ews20.bellgloba l.com...
HI! thanks, thats what I needed to know :)

Paul
"Danny" <da*******@blue bottle.com> wrote in message
news:%g******** **********@news svr29.news.prod igy.net...


All frames parent is actually Window object, not Document, so,
just
do window.myVar=wi ndow.MYIFRAME.m yVarInTheIframe Here 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="JavaS cript">
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 varborderfromif rame=1;
Here you create a global variable varborderfromif rame with a value of 1.

window.varborde rfromiframe=win dow.Iframemain. y;
Then you immediately replace the value with a reference to the 'y'
property of the object referenced by window.Iframema in. Why not:

var varborderfromif rame = window.Iframema in.y;
If window.Iframema in 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 varborderfromif rame = (window.Iframem ain && window.Iframema in.y);
I'll guess that window.Iframema in 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.getEle mentById
&& (o = document.getEle mentById('Ifram emain'))){

// 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( varborderfromif rame)


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"><htm l>
<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.getEle mentById
&& (o = document.getEle mentById('Ifram emain'))){
alert('Iframema in is a ' + o.nodeName
+ '\npassToParent is a : '
+ (o.contentWindo w && typeof o.contentWindow .passToParent)) ;
}
}

</script>
</head>
<body>
<p>
<input type="button" value="Get a variable from the iFrame"
onclick="getIfr ameVar();">
<br>
</p>
<iframe src="z2.html" id="Iframemain " name="Iframemai n"></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="passTo Parent(this.for m.testInput.val ue);">
</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.ne t.au> wrote in message
news:rA******** ********@news.o ptus.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="JavaS cript">


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 varborderfromif rame=1;


Here you create a global variable varborderfromif rame with a value of 1.

window.varborde rfromiframe=win dow.Iframemain. y;


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

var varborderfromif rame = window.Iframema in.y;
If window.Iframema in 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 varborderfromif rame = (window.Iframem ain && window.Iframema in.y);
I'll guess that window.Iframema in 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.getEle mentById
&& (o = document.getEle mentById('Ifram emain'))){

// 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( varborderfromif rame)


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"><htm l>
<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.getEle mentById
&& (o = document.getEle mentById('Ifram emain'))){
alert('Iframema in is a ' + o.nodeName
+ '\npassToParent is a : '
+ (o.contentWindo w && typeof o.contentWindow .passToParent)) ;
}
}

</script>
</head>
<body>
<p>
<input type="button" value="Get a variable from the iFrame"
onclick="getIfr ameVar();">
<br>
</p>
<iframe src="z2.html" id="Iframemain " name="Iframemai n"></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="passTo Parent(this.for m.testInput.val ue);">
</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.ne t.au> wrote in message
news:rA******** ********@news.o ptus.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
8196
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 get the value returned to the parent window. The alert in the parent window always returns undefined errors. Any help would be much appreciated. I call the modal window and wait for the return as such: function LCC()
11
41205
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 enable or disable my submit button in my parent form without using any refresh/reload or submit button in my form. Looks realy simple but i don't know how to get it work. actualy, i've been able to populate listbox,text boxes in my parent with...
4
9515
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 condition is met. In this case, the condition is that an iframe has been reloaded completely. I do this by examining a hidden document variable in the iframe, and when it has changed from "" to "1" is the signal for me that the iframe is loaded. (if...
3
1765
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 ---------------- <html> .... heading tags <body>
2
9872
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
10550
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' ); It is working fine in IE. But in Mozilla Firefox , null value is returned.Is there any way to use hidden variables compatible to browsers......?
2
2227
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 that made sense. THANKS
2
3155
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
0
8861
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.