Thursday
Dec052013

Simple way to avoid caching in Javascript

This is a tried and true method to avoid a situation in which you want to make a call to a static url which may return a different result each time. Simply append an argument to the url with a random or otherwise unique string as the value.

Consider this javascript function:

        function getUniqueString()
        {
            var d = new Date();
            return d.getTime().toString();
        }

This will return a unique string every time that you call it.

Now, suppose you are calling a url that looks like this:

        var url = "http://myserver.com/getSomeImage";

It may work the way you expect in one browser but not in another.  Just append an argument to it like so:

       var url = "http://myserver.com/getSomeImage?cacheKiller=" + getUniqueString();

The url might look like this:

       http://myserver.com/getSomeImage?cacheKiller=1386232969279

Well done!

 

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (29)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    I'm trying to find out what the most popular blogs are when it comes to news and views and cell phone stuff..
  • Response
    Response: groene smoothies
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: afslankpillen
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: watch the video
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: diet foods
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: united cash loans
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: united cash loans
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: zandile williams
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    get the best old cars for sale anywhere
  • Response
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: visit these guys
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Carl Franklin - Intellectual Hedonism - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: obrolan bola
    Carl Franklin - Blog - Simple way to avoid caching in Javascript
  • Response
    Carl Franklin - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: car title loans
    Carl Franklin - Blog - Simple way to avoid caching in Javascript
  • Response
    Response: you can check here
    Carl Franklin - Blog - Simple way to avoid caching in Javascript
  • Response
    o cão mais famoso do instagram com calculadoras online confira. site de calculadoras online
  • Response
    Response: new year 2016
    rhbh
  • Response
    вконтакте
  • Response
  • Response
    Response: Happy Propose Day
    Happy Propose Day 2016: Hii Friends Today I am Going to Share with You a Happy Propose Day Images and Happy Propose Day Status for ...
  • Response
    Buy / Send Valentine's Chocolate Day Gifts for Her / Him to USA, UK, India. Order Online Valentines Chocolate Day Gifts from IGP.com. ✓ Free Shipping in India ...
  • Response
    Response: Renacen
    Good job on this article! I really like how you presented your facts and how you made it interesting and easy to understand. Thank you.
  • Response
    Roland Garros 2016
  • Response
    Response: Menu with jquery
    Expandable menu with jquery,html,css
  • Response
    TDF 2016 live

Reader Comments (1)

I find useful the information. Thank you!

Comments for this entry have been disabled. Additional comments may not be added to this entry at this time.
« C# to get your local IP address when online. | Main | Perfect Low-Carb Chocolate Ice Cream »