Web Development Blog

by Ei Sabai Nyo

21 Apr, 2010

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;
    });
};
Love what you've just read? Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
Your name:   Your email:  

4 Responses to “Troubleshooting jQuery.get() Cache issue in Internet Explorer”

  1. JĂșnior says:

    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. Simon says:

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

    P.S.

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

  3. Roman says:

    Very useful information and help me too.

  4. Eric says:

    Thanks, I’m going to try adding:
    $.ajaxSetup({ cache: false });
    to see if that works first.

Profile PicHello! Welcome to Web development blog! My name is Ei Sabai and on this blog, I write about web development, mobile app development, latest web technologies and the likes. Read more about me or have a look at some of the tips & resources I've written.
Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
We do NOT spam.
Your name:  
Your email:  

Tips & Resources

Tips & Resources
WordPress Web Hosting
Recommended web hosting providers for WordPress 3.0
iPhone Native App Development
Important steps into iPhone app development for beginners
iPhone Web App Development
Tips for iPhone web app development
Coupons for Web Developers
Get discounts on web hosting, domain names, templates, etc
10 Useful jQuery Snippets
Easy-to-use jQuery snippets for any website
HTML Email Newsletter
Step-by-step tutorial on how to code an HTML email newsletter
  • bluehost Hosting $6.95/month
  • Joomla Templates

Recommended Book

Categories