submiting a form inside an iframe from outside the iframe

This one has me stumped (friday afternoon and I am done!).  I am certain it should be done (as long as the iframe content is from the same site).  Yet EVERY example I have found online doesn't work in Firefox (not sure about other browsers).  I have tried many possible variations.  I have other workarounds, but for various reasons I would really like to be able to just have a submit button outside the iframe, that when pressed submits the content of a form inside of the iframe.

Here is just one:

window.frames['myframe'].document.forms['suggestForm'].submit();

Ultimately I get an error saying that there is no submit function, or no properties, etc. 

Has anyone done this and can share their code?

TweetBacks
Comments
Hugo Matinho's Gravatar hey i got this working for you i think. how can i share the code with you ?
# Posted By Hugo Matinho | 9/22/07 12:29 PM
Joshua's Gravatar sweet, just copy/paste it into the comments if you don't mind, so others that search from google and see it too.
# Posted By Joshua | 9/22/07 12:51 PM
Hugo Matinho's Gravatar hey, here's your script it's divided in two parts, this one goes in between the head tags:
<script type="text/javascript">

/**
*
*   SubmitForm function definition:
*   Reference the iFrame: var yourVariable = parent.document.getElementById("iFrameNameHere");
*   Reference to the window object generated by the the iframe: var yourWindowVariable = yourVariable.contentWindow; //other browsers use contentDocument
*   Reference to the form object inside of the iFrame: var theFormObject = yourWindowVariable.document.getElementById("formNameHere");
*   Submit the form now :) theFormObject.submit();
*/

function SubmitForm(){
//make a reference to the iFrame;
var iframeElem = parent.document.getElementById("iFrame");
//make a reference to the window of the iFrame (Note: for Mozilla it's diferent you got to use contentDocument instead of contentWindow);
var win = iframeElem.contentWindow;
//make a reference to the form element in the iframe;
var theForm = win.document.getElementById("form1");
//submit the form
theForm.submit();
//just testing
alert("ok");
}

</script>



and this one goes into the end just before closing the body tag:

<script>
/*
*
*   Make it available in the DOM
*   Reference your submit button
*
*/

var myButton = parent.document.getElementById("submit");

/**
*
*   Older method of attaching event:
*   myButton.onclick = SubmitForm;
*
*/

/**
*
*   IE Hack - attachEvent instead of addEventListener for other browsers
*
*/
myButton.attachEvent('onclick', SubmitForm);
</script>


i've comented the javascript nothing two hard but i hope you understand it
# Posted By Hugo Matinho | 9/22/07 2:51 PM
Hugo Matinho's Gravatar by the way the properties i used here are for IE .. if you need them in firefox check the comments
# Posted By Hugo Matinho | 9/22/07 2:58 PM
Joshua Cyr's Gravatar Hi Hugo,

Thanks very much for posting this. Afraid I am still having the same error, in both IE and FF. I tried it in my own code, but also just used your code on a test page. To be clear, my submit button is on the parent frame, not in the iframe. For whatever reason I just can't get at the properties of the iframe form to submit. I get either an object expected error or a iframeElem has no properties error. Pitty.
# Posted By Joshua Cyr | 9/24/07 9:27 AM
m larsen's Gravatar have you tried explicitly setting the <tt>document.domain</tt> in both the parent and the iframe?
# Posted By m larsen | 9/28/07 1:30 PM
James's Gravatar Hey Dude...

Try this:

var iframe = window.frames[&lt;FRAMEID&gt;].document.forms[0].submit();

You have to remember though, the contents of your iframe need to be fully loaded for this to work.
# Posted By James | 9/20/08 5:03 PM
Peter's Gravatar I doubt you’re still having this problem over 2 years later, but if anyone else finds this page on the web…

That type of error “there is no submit function, or no properties, etc.” can happen when you have an input or button with the name “submit”—change the name to something other than “submit”.
# Posted By Peter | 2/7/10 3:21 PM

NAVIGATION

Home
About Me

RSS


Search

Subscribe

Enter your email address to subscribe to this blog.

Recent Entries

Flash Camp Boston
New Blog Design
Pre-Conference Training at cf.Objective()
FireFox 3.6 KTML Editor Fix
I am now a part of the Adobe Community Professionals Group

Recent Comments

FireFox 3.6 KTML Editor Fix
Fred said: Found another bug in Firefox 3.6 When inserting a table you can't select the number of columns. So I... [More]

Repeating Events Question
ueghbxedu said: UaejcB <a href="http://ysyhrmkbkhco.com/&...;, [url=http://pwncz... [More]

Repeating Events Question
fadxkfyuadn said: n6qVCL <a href="http://bdiorhdtbwzb.com/&...;, [url=http://uvnao... [More]

FireFox 3.6 KTML Editor Fix
Joshua said: While changing that far will load the editor, does it show the drop down class menu correctly now? [More]

FireFox 3.6 KTML Editor Fix
Al Johnson said: HI, I am still fighting to keep my code going as there is nothing better than KTML nad I have writt... [More]

Calendar

Sun Mon Tue Wed Thu Fri Sat
 123456
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31    

Archives By Subject

blogs (31) [RSS]
books (4) [RSS]
Crazy (39) [RSS]
DIY (8) [RSS]
Flex (3) [RSS]
games (10) [RSS]
GRRR (13) [RSS]
Ideas (11) [RSS]
Local (14) [RSS]
LOLpics (2) [RSS]
money (9) [RSS]
music (3) [RSS]
Personal (27) [RSS]
Photos (8) [RSS]
Politics (8) [RSS]
Projects (22) [RSS]
Review (18) [RSS]
RPM (9) [RSS]
Spam (16) [RSS]
Technology (66) [RSS]
Testing (3) [RSS]
TV (15) [RSS]
video (32) [RSS]
Web Dev (218) [RSS]
World of Warcraft (16) [RSS]