Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ajax - how to tell client to render new page.

 
marten kay
Ranch Hand
Posts: 178
Java jQuery Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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?
     
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand
    Posts: 15302
    6
    Chrome IntelliJ IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64629
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    marten kay
    Ranch Hand
    Posts: 178
    Java jQuery Postgres Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic