File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Ajax - how to tell client to render new page. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax - how to tell client to render new page." Watch "Ajax - how to tell client to render new page." New topic

Ajax - how to tell client to render new page.

marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178


I am creating a web interface for an app with these steps:
1. Server generates HTML doc, tracking the id of active elements in a map
2. When the user clicks an active element, the client sends a XMLHttpRequest with the id to the server
3. Server generates a response using the map and other info, sends back to the client either a
  • javascript function in a string, or
  • a new HTML doc for rendering

  • 4. the client then
  • executes the javascript using eval(),or
  • renders the new page (how do I do this?)

  • Question: How to get the client to render the new page when it's expecting a XMLHttpResponse?

    Other Question: Is this OK to do, or is this an evil use of eval?

    when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15302

    You would need to put something in the response that your JavaScript code looks for and decides to render a new page, probably using the window.location. And no, running JavaScript pulled from the server is quite common.

    GenRocket - Experts at Building Test Data
    Bear Bibeault
    Author and ninkuma

    Joined: Jan 10, 2002
    Posts: 63852

    The whole point of Ajax is to not render a new page. How about, instead of returning the HTML for the new page, it returns the URL to the new page, and you do a JavaScript redirect to that page?

    [Asking smart questions] [About Bear] [Books by Bear]
    marten kay
    Ranch Hand

    Joined: Feb 03, 2007
    Posts: 178

    Cool, thanks to both; this has really helped to clean up my code.

    Been reading too much Crockford scaring my on the eval thing. But generating javascript on the server sure is neater.

    Now i'm returning the following to the client when a new document is required:

    and this works in both firefox and IE.

    FYI - This page works a table (changing details, adding/deleting rows etc) where most can be done with ajax, but when a different view is required (different rows), I need to render the new page, so that is where this comes in. Although I am cheating a bit as I am sorting on the server which perhaps I shouldn't.
    I agree. Here's the link:
    subject: Ajax - how to tell client to render new page.
    It's not a secret anymore!