File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes document.open in ajax callback function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "document.open in ajax callback function" Watch "document.open in ajax callback function" New topic
Author

document.open in ajax callback function

Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
Hi,
I am having trouble with this callback function(belos), all I want to do is completely change a document with the results from the server (using document.open() etc), I have simplified this function just for the example, in my app it may be a fragment that needs injecting into the current page. This code works fine on Firefox but IE seems to have a problem when it reaches the document.open method. If anyone knows how to do this or a workaround I'd be very happy. Thanks all.

Sincerly

Stu.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

Originally posted by Stu Johns:
in my app it may be a fragment that needs injecting into the current page
Well which is it? Injecting a fragment is substantially different from replacing the whole document.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
Hi,

The bit giving me trouble is where I am trying to replace the whole document, in an ajax callback function.

thanks

Stu
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
If you are trying to replace the entire page, than why in the world are you using Ajax? A simple post back will do it.

Eric
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
because I dont know until the response comes back from the server that I do need to replace the whole page. I may need to just update the page with some validation failures if that is what is returned from the servlet/jsp. However if a complete page is returned I am trying to display it.

Thanks for helping.

Stu
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
just to add, its IE6 this fails in.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

Not the best way to do it. Have the server return either the validation information or the URL of the page to display, rather than the content of the page itself.
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
as I am using struts to return the page I'd have to somehow override that functionality to just return the URL, which i was hoping to avoid.
thanks again for your help.

Stu.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

Struts or otherwise, you can return whatever you want.

If you are going to be working with Ajax, you need to move beyond the "always return a full HTML page" mentality.

For this type of situation, a JSON construct telling the on-page script what to do is a good approach,
[ August 12, 2008: Message edited by: Bear Bibeault ]
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
Ok, I have been trying stuff based on your suggestion,
Now my app works like this:
1. user submits form data.
2. struts validates, if unsuccessful the errors returned are displayed (by injection on page using ajax)
3. if validation passes then data is saved to the database - at this point the user needs to be redirected to a different page.
The ajax callback function is still waiting for a server response, so the response it now gets is a JSON object name value pair {"theURL", "/webapp/action.do"}
I then use document.location to display the required page.

Do you think this is the best way to do it or have i missed something?

thanks

Stu.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60767
    
  65

Sounds pretty much like how I would do it.
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
Excellent! Thanks for the advice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: document.open in ajax callback function
 
Similar Threads
pass <bean:write> to a function
JSP Action Buttons freez after returning from ajax call
Accessing Servlet by HTML
AJAX question - Struts portlet JSR 168
Jquery ajax issue