468,771 Members | 1,824 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

PASSING VALUE FROM IFRAME TO PARENT.

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 values from
my iframe. But i can't get this button enabled or disabled. Can anyone
tell me what to do please???
Jul 20 '05 #1
11 40847
HolaGoogle wrote:

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.


See below for an example; however, I suspect that your efforts are not
worth the issues they'll cause, it's indeed trivial for a user to just
deactivate javascript or modify script at run-rime to have it work the
way he/she wants ;-)
<script type="text/javascript">
var someHTML="<script type=\"text\/javascript\">"+
"function foo() {"+
" var bReturn=Math.random()<0.5;"+
" document.body.innerHTML='disable ? -> '+bReturn;"+
" return bReturn; };"+
"<\/script>"+
"Hello, World !";
var bar=function(){
var oldHandler, falseHandler=new Function("return false");
var oldAction, falseAction="#";
return function (frm){
var fr=frames["myIframe"];
if(!oldHandler) oldHandler=frm.onsubmit;
if(!oldAction) oldAction=frm.action;
if(fr && fr.foo){
if(fr.foo()){
frm.onsubmit=falseHandler;
frm.action=falseAction;
frm.elements["mySubmitButton"].disabled=true;
}else{
frm.onsubmit=oldHandler;
frm.action=oldAction;
frm.elements["mySubmitButton"].disabled=false;
}
}
}
}();
</script>

<form action="foo.html" onsubmit="return confirm('Really?')">
<input type="button" value="disable?" onclick="bar(this.form)">
<input type="submit" name="mySubmitButton">
<input type="submit">
</form>

<iframe name="myIframe" src="javascript:parent.someHTML"></iframe>
Jul 20 '05 #2
hummm...first of all thanks Yann-Erwan!! thanks for your quick answer
but as you said too much effort for something that simple...Plus i'll
have to submit the form....Well, i was realy hoping that it could be a
way to do something like this in my parent form:

function EnableDisableButton()
{

var fr=frames['myIframe'].document.forms[0];

if(fr["MyIframeValue"].value)
{
document.forms[0].elements["EnableDisable"].disabled=false;
}
}

and refer to myiframe like this in my parent form:
<IFRAME id="myIframe" onload="EnableDisableButton()"
SRC="MyIFrame.asp">
</IFRAME>
and then :
<form name="ParentForm">
<br>
<INPUT TYPE=submit disabled VALUE="Enable/Disable"id=EnableDisable
name=EnableDisable>

But this doesn't work....any idea??

Thanks once again!
Yann-Erwan Perio <y-*******@em-lyon.com> wrote in message news:<3f***********************@news.free.fr>...
HolaGoogle wrote:

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.


See below for an example; however, I suspect that your efforts are not
worth the issues they'll cause, it's indeed trivial for a user to just
deactivate javascript or modify script at run-rime to have it work the
way he/she wants ;-)
<script type="text/javascript">
var someHTML="<script type=\"text\/javascript\">"+
"function foo() {"+
" var bReturn=Math.random()<0.5;"+
" document.body.innerHTML='disable ? -> '+bReturn;"+
" return bReturn; };"+
"<\/script>"+
"Hello, World !";
var bar=function(){
var oldHandler, falseHandler=new Function("return false");
var oldAction, falseAction="#";
return function (frm){
var fr=frames["myIframe"];
if(!oldHandler) oldHandler=frm.onsubmit;
if(!oldAction) oldAction=frm.action;
if(fr && fr.foo){
if(fr.foo()){
frm.onsubmit=falseHandler;
frm.action=falseAction;
frm.elements["mySubmitButton"].disabled=true;
}else{
frm.onsubmit=oldHandler;
frm.action=oldAction;
frm.elements["mySubmitButton"].disabled=false;
}
}
}
}();
</script>

<form action="foo.html" onsubmit="return confirm('Really?')">
<input type="button" value="disable?" onclick="bar(this.form)">
<input type="submit" name="mySubmitButton">
<input type="submit">
</form>

<iframe name="myIframe" src="javascript:parent.someHTML"></iframe>

Jul 20 '05 #3
HolaGoogle wrote:
Well, i was realy hoping that it could be a
way to do something like this in my parent form: But this doesn't work....any idea??


Your approach seems fine and I've got it working on my IE6/offline, see
below. Could you tell me more about what error you get, and on which
platform/browser?

I'd also add some object detection for a better fallback and give your
iframe a name to be able to refer to it from the 'frames' collection
correctly.
--- fr1.html ---
<script type="text/javascript">
function EnableDisableButton() {
var fr=frames['myIframe'];
if(fr && fr.document){
fr=fr.document.forms[0];
if(fr.elements["MyIframeValue"].value) {
document.forms[0].elements["EnableDisable"].disabled=false;
}
}
}
</script>

<form action="foo">
<input type="submit"
disabled="disabled"
name="EnableDisable">
</form>

<iframe name="myIframe"
onload="EnableDisableButton()"
src="fr2.html"></iframe>
--- fr2.html ---
<form action="foo">
<input type="text"
name="MyIframeValue"
value="Hello, World!">
</form>
Regards,
Yep.
Jul 20 '05 #4
Oh my!!! thanks! many thanks Yann-Erwan!!! you did the trick... i
still don<t understand why your way works and mine didn't work at
all???? apart from object detection the only difference i can see is
this line
if(fr["MyIframeValue"].value) and yours
if(fr.elements["MyIframeValue"].value)
anyway....thanks a lot....
Now that this step is done, what i'm trying to do is set value for my
iframe and the button in my parent will be enabled only if this value
is true.
i.e

if(fr.elements["MyIframeValue"].value =='enable') then enable button

as this value is kept on server i,m calling my server method within my
iframe and refresh my iframe every 30 sec to make sure that this value
didn't change. If it does change then the i<ll have to disable the
button.
I'm trying to get this work now...i'm working on it...hope i won't run
into much trouble this time....

but i wonder...is this possible or i'll realy have to refresh the
parent as well to get everything work?? if so, i'm screwed coz' by
refreshing parent, i'll lose every change made since the last save
that's why i came up with this iframe idea to refresh it and get the
new value withouth refreshing the parent...

Thanks a lot your help is more than appreciated!!!

Yann-Erwan Perio <y-*******@em-lyon.com> wrote in message news:<3f***********************@news.free.fr>...
HolaGoogle wrote:
Well, i was realy hoping that it could be a
way to do something like this in my parent form:

But this doesn't work....any idea??


Your approach seems fine and I've got it working on my IE6/offline, see
below. Could you tell me more about what error you get, and on which
platform/browser?

I'd also add some object detection for a better fallback and give your
iframe a name to be able to refer to it from the 'frames' collection
correctly.
--- fr1.html ---
<script type="text/javascript">
function EnableDisableButton() {
var fr=frames['myIframe'];
if(fr && fr.document){
fr=fr.document.forms[0];
if(fr.elements["MyIframeValue"].value) {
document.forms[0].elements["EnableDisable"].disabled=false;
}
}
}
</script>

<form action="foo">
<input type="submit"
disabled="disabled"
name="EnableDisable">
</form>

<iframe name="myIframe"
onload="EnableDisableButton()"
src="fr2.html"></iframe>
--- fr2.html ---
<form action="foo">
<input type="text"
name="MyIframeValue"
value="Hello, World!">
</form>
Regards,
Yep.

Jul 20 '05 #5
HolaGoogle wrote:
as this value is kept on server i,m calling my server method within my
iframe and refresh my iframe every 30 sec to make sure that this value
didn't change. If it does change then the i<ll have to disable the
button.
If you're developing for a specific target (IE/Mozilla) then it'd be
easier to use the XmlHttpRequest object. You can read Jim Ley's page
about this

<URL http://jibbering.com/2002/4/httprequest.html>
but i wonder...is this possible or i'll realy have to refresh the
parent as well to get everything work??
Well it should be perfectly doable without refreshing the parent. The
server should just return something like the following to the iframe:

<script type="text/javascript">
window.onload = function() {
var STOP_SUBMISSION=true; //STOP_SUBMISSION=<%="true"%>
if(STOP_SUBMISSION){
if(parent && typeof parent.disableSubmitButton=="function"){
parent.disableSubmitButton();
}
}
setTimeout("location.reload(true);", 30000);
}
</script>

with the disableSubmitButton defined in the parent, eg
function disableSubmitButton(){
document.forms[0].elements["EnableDisable"].disabled=true;
}
refreshing parent, i'll lose every change made since the last save
that's why i came up with this iframe idea to refresh it and get the
new value withouth refreshing the parent...


Don't forget that people with js disabled (if you're working for the
web) would still be able to submit, so also do your checks server-side.
Cheers,
Yep.
Jul 20 '05 #6
Thanks very much Yep (can i call you Yep??) ..i did in fact get it
work but i was too late to let you know...it's almost the same thing
as you did below.. just a bit different...
just a quick one this time (or once again).....since i got everything
work the way i wanted ( thank to you) i pasted my code in my "real
form" (before i was justing testing...) and now i'm getting some
strange script errors such as:

document.forms.0.elements is null or not object (when i use elements
to refer my button) OR
document.forms.o.enabledisable is null or not object (when i get rid
of the elements....)

Even if I got these errors my button my button will be enabled though.

At first i thought i was because my include files (i have 3 #includes
in my forms) so for my tests i removed them...same thing....
If i put back my includes then everything stops to work...even doing
document.forms[3] which i think is my form number after all my
includes, it still doesn't work...any idea...

i,m sorry to waste your time (particulary if this is so easy and
stupid for you but i'm new with all this and miss some basic stuff)...
your help is realy appreciated!!

Take care!

Yann-Erwan Perio <y-*******@em-lyon.com> wrote in message news:<3f*********************@news.free.fr>...
HolaGoogle wrote:
as this value is kept on server i,m calling my server method within my
iframe and refresh my iframe every 30 sec to make sure that this value
didn't change. If it does change then the i<ll have to disable the
button.


If you're developing for a specific target (IE/Mozilla) then it'd be
easier to use the XmlHttpRequest object. You can read Jim Ley's page
about this

<URL http://jibbering.com/2002/4/httprequest.html>
but i wonder...is this possible or i'll realy have to refresh the
parent as well to get everything work??


Well it should be perfectly doable without refreshing the parent. The
server should just return something like the following to the iframe:

<script type="text/javascript">
window.onload = function() {
var STOP_SUBMISSION=true; //STOP_SUBMISSION=<%="true"%>
if(STOP_SUBMISSION){
if(parent && typeof parent.disableSubmitButton=="function"){
parent.disableSubmitButton();
}
}
setTimeout("location.reload(true);", 30000);
}
</script>

with the disableSubmitButton defined in the parent, eg
function disableSubmitButton(){
document.forms[0].elements["EnableDisable"].disabled=true;
}
refreshing parent, i'll lose every change made since the last save
that's why i came up with this iframe idea to refresh it and get the
new value withouth refreshing the parent...


Don't forget that people with js disabled (if you're working for the
web) would still be able to submit, so also do your checks server-side.
Cheers,
Yep.

Jul 20 '05 #7
HolaGoogle wrote:
Thanks very much Yep (can i call you Yep??)
Sure you can, only my boss and family use my first name :-)
document.forms.0.elements is null or not object
Okay, debug time!

First of all, if your connection allows it, I'd suggest that you
download and install Mozilla, its javascript console indeed gives
meaningful error messages, unlike IE.

Now, you've got an error probably because the document.forms[0] is
undefined, and that you're trying to read the "elements" property on it.
My bet is that you don't reference the correct document, since you're
working with frames, are your includes in the correct page? Also check
if something like

MyFrame.document.forms[0].elements["hello"]

wouldn't help. If you still have issues, then it would be best if you
could post a link to the page, so that we can reproduce the error.
i,m sorry to waste your time (particulary if this is so easy and
stupid for you but i'm new with all this and miss some basic stuff)...


You're certainly not wasting my time, if I were bored I'd be elsewhere,
believe me :-) So don't be so shy, it's perfectly okay to ask so-called
"basic" questions, we all have to learn from somewhere, the important
thing is to have fun while learning.
Cheers,
Yep.
Jul 20 '05 #8
I think i got now!! i took off all my form tags i had in my includes
files (the files are used when i call my methods running at server so
no need to put <form></form>) and got everything work now!!

thanks very much for your kind help and time!

ho*****@yahoo.com (HolaGoogle) wrote in message news:<cd*************************@posting.google.c om>...
Thanks very much Yep (can i call you Yep??) ..i did in fact get it
work but i was too late to let you know...it's almost the same thing
as you did below.. just a bit different...
just a quick one this time (or once again).....since i got everything
work the way i wanted ( thank to you) i pasted my code in my "real
form" (before i was justing testing...) and now i'm getting some
strange script errors such as:

document.forms.0.elements is null or not object (when i use elements
to refer my button) OR
document.forms.o.enabledisable is null or not object (when i get rid
of the elements....)

Even if I got these errors my button my button will be enabled though.

At first i thought i was because my include files (i have 3 #includes
in my forms) so for my tests i removed them...same thing....
If i put back my includes then everything stops to work...even doing
document.forms[3] which i think is my form number after all my
includes, it still doesn't work...any idea...

i,m sorry to waste your time (particulary if this is so easy and
stupid for you but i'm new with all this and miss some basic stuff)...
your help is realy appreciated!!

Take care!

Yann-Erwan Perio <y-*******@em-lyon.com> wrote in message news:<3f*********************@news.free.fr>...
HolaGoogle wrote:
as this value is kept on server i,m calling my server method within my
iframe and refresh my iframe every 30 sec to make sure that this value
didn't change. If it does change then the i<ll have to disable the
button.


If you're developing for a specific target (IE/Mozilla) then it'd be
easier to use the XmlHttpRequest object. You can read Jim Ley's page
about this

<URL http://jibbering.com/2002/4/httprequest.html>
but i wonder...is this possible or i'll realy have to refresh the
parent as well to get everything work??


Well it should be perfectly doable without refreshing the parent. The
server should just return something like the following to the iframe:

<script type="text/javascript">
window.onload = function() {
var STOP_SUBMISSION=true; //STOP_SUBMISSION=<%="true"%>
if(STOP_SUBMISSION){
if(parent && typeof parent.disableSubmitButton=="function"){
parent.disableSubmitButton();
}
}

setTimeout("location.reload(true);", 30000);
}
</script>

with the disableSubmitButton defined in the parent, eg
function disableSubmitButton(){
document.forms[0].elements["EnableDisable"].disabled=true;
}

refreshing parent, i'll lose every change made since the last save
that's why i came up with this iframe idea to refresh it and get the
new value withouth refreshing the parent...


Don't forget that people with js disabled (if you're working for the
web) would still be able to submit, so also do your checks server-side.
Cheers,
Yep.

Jul 20 '05 #9
Oh mmy God!!! Yep you're so sweet!! thanks a lot for your help...as i
posted it yesterday, i got it work but i'm going to download the
Mozilla thing ;-) as you suggested! if it can help why not???
For sure i'm having soo much fun even if sometimes i do get
discouraged i'm hang in there ;-)....
One last question : Are you in France??(em-lyon.com) ..if so do
youspeak french...coz< i,m in canada and as you might noticed my
english is realy bad ;-)

Take care et bon week end!!
Yann-Erwan Perio <y-*******@em-lyon.com> wrote in message news:<3f*********************@news.free.fr>...
HolaGoogle wrote:
Thanks very much Yep (can i call you Yep??)


Sure you can, only my boss and family use my first name :-)
document.forms.0.elements is null or not object


Okay, debug time!

First of all, if your connection allows it, I'd suggest that you
download and install Mozilla, its javascript console indeed gives
meaningful error messages, unlike IE.

Now, you've got an error probably because the document.forms[0] is
undefined, and that you're trying to read the "elements" property on it.
My bet is that you don't reference the correct document, since you're
working with frames, are your includes in the correct page? Also check
if something like

MyFrame.document.forms[0].elements["hello"]

wouldn't help. If you still have issues, then it would be best if you
could post a link to the page, so that we can reproduce the error.
i,m sorry to waste your time (particulary if this is so easy and
stupid for you but i'm new with all this and miss some basic stuff)...


You're certainly not wasting my time, if I were bored I'd be elsewhere,
believe me :-) So don't be so shy, it's perfectly okay to ask so-called
"basic" questions, we all have to learn from somewhere, the important
thing is to have fun while learning.
Cheers,
Yep.

Jul 20 '05 #10
HolaGoogle wrote:
Oh mmy God!!! Yep you're so sweet!! thanks a lot for your help...as i
posted it yesterday, i got it work but i'm going to download the
Mozilla thing ;-) as you suggested! if it can help why not???
<blush>
Well Mozilla offers several useful tools, like the js console (the one I
use most), the debugger and the DOM inspector. You wouldn't necessarily
use them all unless you're doing big DOM/js development, but they can
come very handy in some situations.
</blush>

(and of course, Mozilla is an excellent browser!)
One last question : Are you in France??(em-lyon.com) ..if so do
youspeak french...coz< i,m in canada and as you might noticed my
english is realy bad ;-)


Non non, simplement l'accent :-)
Cordialement,
Yep (Paris).
Jul 20 '05 #11
Oh mmy God!!! Yep you're so sweet!! thanks a lot for your help...as i
posted it yesterday, i got it work but i'm going to download the
Mozilla thing ;-) as you suggested! if it can help why not???
For sure i'm having soo much fun even if sometimes i do get
discouraged i'm hang in there ;-)....
One last question : Are you in France??(em-lyon.com) ..if so do
youspeak french...coz< i,m in canada and as you might noticed my
english is realy bad ;-)

Take care et bon week end!!
Yann-Erwan Perio <y-*******@em-lyon.com> wrote in message news:<3f*********************@news.free.fr>...
HolaGoogle wrote:
Thanks very much Yep (can i call you Yep??)


Sure you can, only my boss and family use my first name :-)
document.forms.0.elements is null or not object


Okay, debug time!

First of all, if your connection allows it, I'd suggest that you
download and install Mozilla, its javascript console indeed gives
meaningful error messages, unlike IE.

Now, you've got an error probably because the document.forms[0] is
undefined, and that you're trying to read the "elements" property on it.
My bet is that you don't reference the correct document, since you're
working with frames, are your includes in the correct page? Also check
if something like

MyFrame.document.forms[0].elements["hello"]

wouldn't help. If you still have issues, then it would be best if you
could post a link to the page, so that we can reproduce the error.
i,m sorry to waste your time (particulary if this is so easy and
stupid for you but i'm new with all this and miss some basic stuff)...


You're certainly not wasting my time, if I were bored I'd be elsewhere,
believe me :-) So don't be so shy, it's perfectly okay to ask so-called
"basic" questions, we all have to learn from somewhere, the important
thing is to have fun while learning.
Cheers,
Yep.

Jul 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Xerxes | last post: by
3 posts views Thread by hendedav | last post: by
3 posts views Thread by Kieran Benton | last post: by
2 posts views Thread by Yazeed_Roumer | last post: by
1 post views Thread by CARIGAR | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.