Snippets, Troubleshooting, Tutorials

Troubleshooting jQuery.get() Cache issue in Internet Explorer

There is an issue with jQuery.get() in Internet Explorer when trying to update content on the fly because IE caches XMLHttpRequest response and the AJAX call made via jQuery.get() is therefore always returning the same result. I encountered this problem when I was writing a function similar to facebook Like feature where the Like counter increments by 1 whenever someone clicks on the I like it link. It works all fine and well on Firefox, but when I tested it on IE, the counter doesn’t increase as expected.

To solve the issue, I added a parameter to the request URL that is unique for each request by using Javascript getTime function. Javascript getTime function returns the number of milliseconds since January 1, 1970. This prevents IE from returning the same/cached response because the request URL is always unique.

Javascript

jQuery.fn.likeIt = function() {
    var r = new Date().getTime(); // unique random number to workaround IE cache issue
    $("a", this).click(function() {
        // Note: my href already has a parameter which is why I have & instead of ? before the r parameter 
	$.get($(this).attr("href") + "&r=" + r, function(data) {  
             // do your processing here
	}); // $.get
    return false;
    });
};

4 thoughts on “Troubleshooting jQuery.get() Cache issue in Internet Explorer

  1. Thanks man! It solved my problem. I had this problem once and fixed with another way a long time ago, but i didnt remember. I was searching in google for the solution and found you.

    Look, i had a problem with this line

    “$.get($(this).attr(“href”) + “&r=” + r, function(data) {”

    Because as i dont know what your getting from the href i didnt change the “&r”, which cause an error, i had to change to “?r” because it was the first variable in my url. Its a simple mistake but it didnt work hehe…

    Well, Thanks again for your solution!

  2. Thank you for the tip! It solved my problem too.

    P.S.

    IE – is the application intended to download a browser..

Comments are closed.

Career Guide for Software Developers