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>