Disable Cache in jQuery

0 min read 190 words

I usually run into this problem when dealing with good ol’ Internet Explorer (..any version of IE actually).

The problem is that IE tries to be smart and not tell you the new output of a file it fetches more than one time with the same filename, instead it shows you what it saw the first time it loaded that file.

You can imagine this would be insanely dumb if you were using a realtime application data source where a script output a different resultset each time, e.g. most database ajax requests.

So what you need to do is just disable caching in jQuery.

$.ajaxSetup ({
    cache: false
});

You don’t have to add this inside your document ready function but I like to incase the jQuery library isn’t finished loading yet for any particular reason, so if the above throws a warning/error then do this:

$(function() {
    $.ajaxSetup ({
        cache: false
    });
});

Essentially what this does is it adds a random number to the request uri as a get variable, by this being different each time it forces the browser to return the new output. e.g:

http://example.com/path/to/some/script/somes_script?_=1337877420424
```

            
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Ai
API
Aws
C
C
Cdk
CLI
Css
Dsp
Eks
Ffi
Gcp
Git
Go
Iac
Ide
Iot
JWT
Llm
MAC
Ml
Npm
Php
Pip
S3
Seo
Sli
Slo
SQL
Sre
SSH
Ssl
Vpc
Wsl

Recent Posts