Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted underscore and unix timestamp appearing after javascript include

On pages that have been loaded using jquery's load function, all javascript files included have an argument '_' with a value equal to the current unix timestamp plus 3 extra numbers when they are loaded.

E.g., If I include "file.js", the actual file that gets included will be "file.js?_=1378360893522".

It is preventing the javascript files from caching, is there any way to stop this behaviour?

Edit: As requested here is the relevant code:

index.html:

<html>
    <head>
        <script type="text/javascript" src="js/jquery.min.js"></script>
    </head>
    <body>
        <div id='new-page'></div>
    </body>
    <script>
        $(document).ready(function() {
            $('#new-page').load("another-page.html");
        });
    </script>
</html>

another-page.html:

<html>
    <head>
        <script type="text/javascript" src="js/another-js-file.js"></script>
    </head>
    <body>
    </body>
</html>

The "another-js-file.js" is loaded as "another-js-file.js?_=1378425747710"

Second edit: This was answered. For those reading this I changed my load call to be something more like:

$.ajax({
    url: "another-page.html",
    cache: true,
    dataType: "html",
    success: function(data){
        $("#new-page").html(data);
    }
});

Some people stated that certain plugins may set cache to be false via ajaxSetup, so it might be necessary to use this before an ajax call you want cached:

$.ajaxSetup({cache:true});
like image 736
Chris Avatar asked Sep 05 '13 23:09

Chris


1 Answers

Your problem is well documented and a solution can be taken from:

jQuery version 1.5 - ajax - <script> tag timestamp problem

The solution involves using the cache option in the jQuery AJAX call.

like image 93
Marc Audet Avatar answered Sep 18 '22 13:09

Marc Audet