aspose file tools*
The moose likes Servlets and the fly likes Servlet writes to iFrame...unable to read contents in IE7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet writes to iFrame...unable to read contents in IE7" Watch "Servlet writes to iFrame...unable to read contents in IE7" New topic
Author

Servlet writes to iFrame...unable to read contents in IE7

Dan Donahue
Greenhorn

Joined: May 10, 2004
Posts: 3
I have a form with action -> servlet and target -> iframe. The onload event for the iframe is set to a function that simply reads the text content of the frame:
[
<form method="post" src="MyServlet" target="uploadFrame" enctype="multipart/form-data" id="mainUploadForm">
....
</form>
<IFRAME id="uploadFrame" onload="resultUpload()" style="display:inline" name="uploadFrame" ></IFRAME>
<script>
document.getElementById("uploadFrame").onload = function() {doShowFrameContent(); }
</script>
]

Servlet processes form data and writes some text data (using response.writer) into the target/response:
[
...MyServlet code. doing some JSON stuff..
resp.getWriter().println(jsonStringer.toString());
]

Original calling page reads the iframe contents and displays text in an alert message.

Works fine in Firefox.

In IE7, data is written to the iframe (right-click on iframe, select "View Source", and I see the data in notepad).
But when I access the data, it returns empty html.

[
right-click on frame, and I see JSON data: {"result":"true","validDevices":0,"errorDevices":0,"companyDupes":26,"updatedDevices":0}
however, jscript code: alert(document.getElementById('uploadFrame').contentWindow.document.documentElement.outerHTML );
returns: <HTML><HEAD></HEAD><BODY> </BODY></HTML>
]

Note: I've used innerHTML as well, with the same result.

How can I access the {"result".....} data written to that iframe? "View Source" is doing it, how can I?

This is driving me nuts!!! Help!!!


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Dan, you are registering two methods on the onLoad event of the frame. 1) resultUpload() and 2) function() {doShowFrameContent(); }

Now it would be great if you could give us code for these two methods and what you are doing in them. Can you see the text as it is in Firefox i.e. are you seeing this in firefox

{"result":"true","validDevices":0,"errorDevices":0,"companyDupes":26,"updatedDevices":0}


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Dan Donahue
Greenhorn

Joined: May 10, 2004
Posts: 3
Hello, Ankit

The discrepency between onload event function names was a typo...I was trying to make the code a little clearer in my post by giving the function name something more illustrative. The onload function is called "resultUpload()" in all cases.


However, I think I found the problem...or at least a workaround. I found some information (I do not have the reference here) about the "target=" attribute in forms. Apparently, if you have something like I have, that is a target with NO extension, (target='uploadFrame'), then IE becomes confused and is unable to interpret the response output correctly. IE uses the extension on the target name to determine how to handle any response text. Without an extension, IE makes (poor) assumptions.

When I changed the name of the iframe from 'uploadFrame' to 'uploadFrame.html', and change my form tag to include target='uploadFrame.html' , then I was able to see the expected response output in the frame in both IE and Firefox. (Firefox had always worked as expected.)
 
jQuery in Action, 2nd edition
 
subject: Servlet writes to iFrame...unable to read contents in IE7