Hello, everyone!
I'm fairly new to this forum, but I spent some time mining it for information. Unfortunaltely, as far as I have searched it I was unable to find an answer to my question. Googling hasn't satisfactorily answered my question also.
Here's the goal:
- - user fills in a form (selects files for upload, writes a post, etc.) and submits it
- - without page reload she gets a response from server (error, unsupported file type, inappropriate language, success, etc.)
I imagine first, submit-without-reload, part is done via the form.target property like this:
- iframe is either created dynamically with JS createElement or statically. But in any case it should look something like this: <iframe id="#iframe123456" name="#iframe123456"></iframe>
- form, also created either dynamically or statically:
<form action="process.php" target="#iframe123456">
<!-- inputs of various types -->
</from>
As for the response/result part, server dumps its reply into the targeted iframe, so I have three alternatives:
- - use iframe's onLoad (onReadyStateChange) event: attach a get_data() function to it.
- - frequently/manually poll iframe for data using setInterval(get_data, milliseconds).
- - inject some script into the server response that makes iframe itself call get_data() .
I am puzzled as to which alternative is the most preferrable, efficient and browser independent. Which one would you recommend?
And another question is how I can check if an iframe has data in it.
I use the following script to get the iframe's inner document object (excerpt from get_data() fucntion ):
if ( iframe.contentDocument ){
doc = iframe.contentDocument;
}else if( iframe.contentWindow ){
doc = iframe.contentWindow.document;
}else if ( document.frames ){
doc = document.frames[iframe.name].document;
}else if( window.frames[iframe.name] ){
doc = window.frames[iframe.name].document;
}
Where iframe = document.createElement("iframe");
And then i check whether doc.body && doc.body.innerHTML exist and are nonempty by this snippet:
/* in IE doc.readyState is bogus: immediately 'complete' */
if ( !( !doc.readyState ||
doc.readyState && doc.readyState == 'complete' ) )
{ /*die*/}
if ( !( doc.body && doc.body.innerHTML ) )
{ /*die*/}
/* get the response */