stillinbeta
stillinbeta

Reputation: 1997

Internet Explorer 7 Ajax links only load once

I'm writing an application and I'm trying to tie simple AJAX functionality in. It works well in Mozilla Firefox, but there's an interesting bug in Internet Explorer: Each of the links can only be clicked once. The browser must be completely restarted, simply reloading the page won't work. I've written a very simple example application that demonstrates this.

Javascript reproduced below:

var xmlHttp = new XMLHttpRequest();

/*
item: the object clicked on
type: the type of action to perform (one of 'image','text' or 'blurb'
*/
function select(item,type)
{

    //Deselect the previously selected 'selected' object
    if(document.getElementById('selected')!=null)
    {
        document.getElementById('selected').id = '';
    }
    //reselect the new selcted object
    item.id = 'selected';

    //get the appropriate page
    if(type=='image')
        xmlHttp.open("GET","image.php");
    else if (type=='text')
        xmlHttp.open("GET","textbox.php");
    else if(type=='blurb')
        xmlHttp.open("GET","blurb.php");

    xmlHttp.send(null);
    xmlHttp.onreadystatechange = catchResponse;

    return false;

}
function catchResponse()
{
    if(xmlHttp.readyState == 4)
    {
        document.getElementById("page").innerHTML=xmlHttp.responseText;
    }

    return false;
}

Any help would be appreciated.

Upvotes: 13

Views: 8254

Answers (8)

Alfredo Carrillo
Alfredo Carrillo

Reputation: 316

Using Dojo, this can be done using dojo.date.stamp, just adding the following to the url:

"...&ts=" + dojo.date.stamp.toISOString(new Date())

Upvotes: 1

Erel Segal-Halevi
Erel Segal-Halevi

Reputation: 36745

In jQuery.ajax, you can set the "cache" setting to false:

http://api.jquery.com/jQuery.ajax/

Upvotes: 0

Kevin Hakanson
Kevin Hakanson

Reputation: 42200

The response header that has worked best for me in the IE AJAX case is Expires: -1, which is probably not per spec but mentioned in a Microsoft Support Article (How to prevent caching in Internet Explorer). This is used in conjunction with Cache-Control: no-cache and Pragma: no-cache.

Upvotes: 1

Amareswar
Amareswar

Reputation:

Read No Problems section in [link text][1] [1]: http://en.wikipedia.org/wiki/XMLHttpRequest

Upvotes: 1

BuB
BuB

Reputation:

xmlHttp.open("GET","blurb.php?"+Math.random();

I agree with this one.. it works perfectly as a solution to this problem. the problem is that IE7's caching of urls were terrible, ignoring the no-cache header and save the resource to its cache using its url as key index, so the best solution is to add a random parameter to the GET url.

Upvotes: 0

jcampbell1
jcampbell1

Reputation: 4529

The problem is that IE does wacky things when the response handler is set before open is called. You aren't doing that for the first xhr request, but since you reuse the xhr object, when the second open is called, the response handler is already set. That may be confusing, but the solution is simple. Create a new xhr object for each request:

move the:

var xmlHttp = new XMLHttpRequest();

inside the select function.

Upvotes: 3

TJ L
TJ L

Reputation: 24462

This happens because Internet Explorer ignores the no-cache directive, and caches the results of ajax calls. Then, if the next request is identical, it will just serve up the cached version. There's an easy workaround, and that is to just append random string on the end of your query.

 xmlHttp.open("GET","blurb.php?"+Math.random();

Upvotes: 16

pkaeding
pkaeding

Reputation: 37633

It looks like IE is caching the response. If you either change your calls to POST methods, or send the appropriate headers to tell IE not to cache the response, it should work.

The headers I send to be sure it doesn't cache are:

Pragma: no-cache
Cache-Control: no-cache
Expires: Fri, 30 Oct 1998 14:19:41 GMT

Note the expiration date can be any time in the past.

Upvotes: 6

Related Questions