Making Internet Explorer and Prototype’s Ajax.PeriodicalUpdater actually work

I’ve been working on a personal project that allows any number of users to submit messages to a server that are then stored in a database. In order to prevent page refreshes, I’m using Ajax to post messages and receive messages from the server. For message retrieval, the client-side code periodically pings the server and grabs any new messages and then appends them to the existing DOM.

Since users will be sending messages at different times, I wanted to make sure that the list of messages would constantly be updated regardless of if the user has posted any messages. To accomplish this, I’m using Prototype’s Ajax.PeriodicalUpdater. Since no data is actually being sent to the server, I was using an HTTP GET request to get all of the new messages. It was working as expected in Safari and Firefox; however, it wasn’t working at all in Internet Explorer. The reason being that, by default, IE caches URLs in order to make surfing faster. This can actually be disabled, but who would expect users to change a default setting like this?

In order to combat this, I had originally tried passing a token in the query string of the request URL using Math.random(). The idea behind this was that the URL would always be unique, so IE’s caching behavior would be irrelevant; however, the specified URL for requests is statically maintained internally by Prototype, so the random token is generated only once. Thus, the URL is always the same. Ultimately, I resolved to change the method by which the PeriodicalUpdater was retrieving messages from an HTTP GET to an HTTP POST. After that, all works well.

Basically, this is the only code that is required:

var sGetMsgUrl = 'get_messages.php';
new Ajax.PeriodicalUpdater('MessageContainer', sGetMsgUrl, {
    method: 'post', // using POST to combat IE caching,
    frequency: 3
});

After that, the asynchronous updating works across all of the browsers.


 
 
 

5 Responses to “Making Internet Explorer and Prototype’s Ajax.PeriodicalUpdater actually work”

  1. Nick
    23. September 2008 at 13:16

    I’m getting the same problem; however, my method is already post. Perhaps this isn’t due to caching issues, but it does seem most likely.

    Basically, my PeriodicalUpdater simply never runs unless I notify IE by some…extreme…means, such as putting an alert INSIDE the updater (which, of course, is a horrible, horrible thing, but serves for debugging).

    If you’ve got any ideas, though, I’m all ears!

  2. Tom
    23. September 2008 at 13:19

    Hey Nick,

    What version of IE is resulting in this? Also, I’m assuming that your periodical updater contained in some function - does that function have any other work that it’s supposed to be doing? Lastly, when are you attempting to start firing the updater?

    Tom

  3. serg
    26. September 2008 at 06:29

    Hi Tom,
    i faced with the same problem ( Nick discribed):
    looks like periodical updater simply never runs in IE =(.
    The IE version i used is 7 (7.0.5730.13).

  4. Rookee
    24. October 2008 at 03:50

    Hey,

    I got the error with the cache in IE and a replace get into post and it work. I tested with IE 6.0 here my Code:

    new Ajax.PeriodicalUpdater(’result’,'/Head/xml.do?sid=’ + Math.random(),
    {
    method:’post’,
    parameters: { htId: ‘39′},
    frequency: 3,
    //insertion: Insertion.Bottom,
    onSuccess: function(transport){
    var response = transport.responseText || “no response text”;
    alert(${”result”}.innerHTML);
    },
    onFailure: function(){ alert(’Something went wrong…’) }
    });

    Hope sombody helps…

  5. J3njy
    24. October 2008 at 08:55

    This is full functional Workaround :

    header( “Expires: Mon, 26 Jul 1997 05:00:00 GMT” ); // disable IE caching
    header( “Last-Modified: ” . gmdate( “D, d M Y H:i:s” ) . ” GMT” );
    header( “Cache-Control: no-cache, must-revalidate” );
    header( “Pragma: no-cache” );

Leave a Reply