AJAX Cache Problem In IE

Friday, April 17, 2009 14:59
Posted in category PHP, Tutorial

Here comes the pain again! IE6. IE6 is a pain to general user but it has caused greater pain to web developers and web designers. IE6 was released in 2001 and it is almost a decade, we are still using it. Seriously, we need to upgrade. If you please, you can join the stop IE6 campaign at http://www.stopie6.org/

Recently a friend of mine had a problem with IE6 and AJAX. The problem was IE6 caching pages fetched with AJAX. Everyone faces this problem once in a while. If you are an AJAX Developer then you should have noticed it quite often with IE6. In this small tutorial and Tip, I will discuss about the problem and its solution.

IE6 generally caches pages fetched with AJAX. e.g. If you pull a file with url http://www.fetchajax.com/test/cache.php with AJAX, IE6 stores this file locally in cache. And next time when you fetch the same url http://www.fetchajax.com/test/cache.php, it retrieves it from the cache. This has both good and bad aspects. If you want the same data, everytime you call the url then it is good to save it to cache. It saves time and yes bandwidth. But if you are developing web application that displays a result in real time, then it is a big problem. A friend of mine recently faced a similar problem. He wrote a small code for ticker that displays information on stocks in real time. He tested it in most of the latest browser. Everything was going smooth in almost all browser. But to his dismay, his code was acting weird in IE6. The data in the ticker did not change. Hmm! A big problem.

What can be the simplest solution? Yes, we can change the url and fetch the data. How about adding a timestamp in the url. Adding a timestamp or any other arbitrary variable to url that changes with time is a good solution to this problem. This random arbitrary variable will cause the IE6 to fetch the file from the server as if it was a different url.

Our earlier url http://www.fetchajax.com/test/cache.php can be changed to http://www.fetchajax.com/test/cache.php?ms=1417687 and then fetched with AJAX. This time IE will not find this file in cache and will request the file from the server.

I have written a small javascript that will attach a different timestamp to the url. You can use it without hesitation. I encourage you to copy.

function uncacheURL(url)
{
    var dt = new Date();
    var t = dt.getTime();

    return url + ‘&ms=’+t;
    }

Just pass the url to this function and you are ready to fetch the page with AJAX in IE.

Happy Reading ;)

VN:F [1.8.3_1051]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.3_1051]
Rating: 0 (from 0 votes)
http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/digg_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/reddit_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/delicious_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/blogmarks_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/google_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/myspace_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/facebook_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/yahoobuzz_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/twitter_32.png
You can leave a response, or trackback from your own site.

Leave a Reply