tags:

views:

78

answers:

1

Hi,
In a page I have this HTML code:

 
<div id="content">
    <div class="container">
        <div class="author">@Francesc</div>
        <div class="message">Hey World!</div>
        <div class="time">13/06/2010 11:53 GMT</div>
    </div>
    <div class="container">
        <div class="author">@SomeOtherUser</div>
        <div class="message">Bye World!</div>
        <div class="time">13/06/2010 14:53 GMT</div>
    </div>
    <div class="container">
        <div class="author">@Me</div>
        <div class="message">Hey World!</div>
        <div class="time">13/06/2010 18:53 GMT</div>
    </div>
</div>
I want to ask, how to get a JSON file, from the server that has more recent messages and put them to the top, I mean above the first <div class="container">.

Another question, it's possible to pass with GET when submiting the request to the server, the time of last update? How can I do it?
Thanks.

+2  A: 

I don't know how your server is serving up the new data.

Given a static text file called new_data.json in the same directory as your page, you could make the following ajax request.

(If you're serving the page from the filesystem, some browsers may give you a little trouble. Safari should work.)

Content of new_data.json file:

[ {"author":"@newAuthor","message":"newMessage","time":"newTime"},
  {"author":"@anotherAuthor","message":"anotherMessage","time":"anotherTime"} 
]

jQuery:

  // Stores the latest request timestamp
var lastRequestTime = new Date();

  // Make ajax request
$.ajax({
        // URL of data, with the last time
    url: 'new_data.json?time='+lastRequestTime,
    dataType:'json',
    success: function(data) {
             // Update the lastRequestTime
        lastRequestTime = new Date();

            // Get the length of the array returned
        var length = data.length;

            // Walk backward through the array, adding each new item 
            //    to the top of the container
        while(length--) {
                 // Create new .container div
            $('<div/>', {className:'container'})

                 // Append new divs to the $container with proper class and data.
                 // data[length][...] uses the current index stored in the length variable
                .append( $('<div/>', {className:'author', text:data[length]['author']} ) )
                .append( $('<div/>', {className:'message', text:data[length]['message']} ) )
                .append( $('<div/>', {className:'time', text:data[length]['time']} ) )

                 // Prepend $container to the #content div
                .prependTo( '#content' );
        }
    }
});
patrick dw
This will work, if I JSON is like: {"author":"@newAuthor","message":"newMessage","time":"newTime"}{"author":"@newAuthor","message":"newMessage","time":"newTime"}Will this put the first in the JSON at the top, and the last in the JSON, in the bottom of the AJAX added?
Francesc
@Francesc - If you have several JSON objects in the document, you will need to put them in an array, and access them one by one in a loop. There are a few different ways to make sure that they end up at the top in the correct order. I'll update my answer in a few minutes with a method that will walk through the array backwards.
patrick dw