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 ?

user198989
  • 4,574
  • 19
  • 66
  • 95
  • 1
    [This](http://stackoverflow.com/questions/1341089/using-meta-tags-to-turn-off-caching-in-all-browsers) might help you – J Santosh Aug 31 '15 at 04:15
  • Yeah but that needed to be added to the remote site's page.If I could, I would first remove the "?" protection :) – user198989 Aug 31 '15 at 04:24
  • possible duplicate of [Jquery getScript caching](http://stackoverflow.com/questions/12884097/jquery-getscript-caching) – Alexis Tyler Aug 31 '15 at 06:04

2 Answers2

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
});
baao
  • 71,625
  • 17
  • 143
  • 203
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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 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 Aug 31 '15 at 04:14
  • @user198989 - OK, that's a little clearer description. Then you have to fix the problem with caching headers from the server. – jfriend00 Aug 31 '15 at 04:15
  • There must be something on the browser side. Maybe some meta tag to prevent cavhe. – user198989 Aug 31 '15 at 04:16
  • @user198989 - you can use `` tags in the page, but that's also a server-side thing. By the time you would add a `` tag via Javascript, the page will already be in the cache as it came from the server. – jfriend00 Aug 31 '15 at 04:17