0

I use this to fetch script;

$.getScript("http://www.example.org/");

However, I dont want it to be cached. Means that if I use getScript again, the script should fetch it again.

This one works in theory;

$.getScript("http://www.example.org/?" + Math.random());

But in practically, it's not. Because the "?" is disabled on the remote site url, so my question is, is there any otherway to tell browser to not cache ?

3
  • 1
    This might help you Commented Aug 31, 2015 at 4:15
  • Yeah but that needed to be added to the remote site's page.If I could, I would first remove the "?" protection :) Commented Aug 31, 2015 at 4:24
  • possible duplicate of Jquery getScript caching Commented Aug 31, 2015 at 6:04

2 Answers 2

2

Recreate the function for your needs:

(function () {
    $.getScript = function(url, callback) {
        $.ajax({
                type: "GET",
                url: url,
                success: callback,
                dataType: "script",
                cache: false
        });
    };
})();

Now it won't cache anymore when you call the function like

$.getScript('script.js', function()
{
    // non cached script.js
});
Sign up to request clarification or add additional context in comments.

1 Comment

If you do this, as the logs I see, it adds the same. Like this /?=_768472138423
0

The remote site cannot disable the effectiveness of "http://www.example.org/?" + Math.random() to prevent caching.

The point of the "?" + Math.random() is to create a unique URL that will not be in the local browser cache. It is the local browser that makes this caching decision and, for local browser caching, it does not matter if the remote site is ignoring the "?" + Math.random() part of the URL or not.

FYI, another way to address caching is for your server to return proper cache headers when this script is retrieved that instruct the browser to never cache this file.

4 Comments

They disabled "?" on the site url structure. So if you try to display like this, example.org/index.php?123 , it says forbidden. Because they disabled "?"
@user198989 - OK, that's a little clearer description. Then you have to fix the problem with caching headers from the server.
There must be something on the browser side. Maybe some meta tag to prevent cavhe.
@user198989 - you can use <meta> tags in the page, but that's also a server-side thing. By the time you would add a <meta> tag via Javascript, the page will already be in the cache as it came from the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.