File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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

Dan Donahue

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">
<IFRAME id="uploadFrame" onload="resultUpload()" style="display:inline" name="uploadFrame" ></IFRAME>
document.getElementById("uploadFrame").onload = function() {doShowFrameContent(); }

Servlet processes form data and writes some text data (using response.writer) into the target/response:
...MyServlet code. doing some JSON stuff..

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

Joined: Aug 03, 2008
Posts: 9462

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


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

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.)
I agree. Here's the link:
subject: Servlet writes to iFrame...unable to read contents in IE7
It's not a secret anymore!