1

On page load, I'm loading an html page through jquery ajax. This html page contains is a "view" and is rendered in the "main contents" div of the site layout. sample view html that is loaded:

<div>hello world</div>
<script src="/javascript.js" type="text/javascript"></script>

The problem is that Firefox loads the javascript.js by adding a unique querystring parameter to it...preventing it from being cached.

Firebug shows: GET http://nodejs/javascript.js?=_1324005635768

This only happens for the javascript loaded dynamically in the view. The javascripts in the site layout load and cache just fine. Firebug shows that jquery ajax returns the normal view without the querystring parameter therefore it's firefox that's adding it.

How do I fix this?

3
  • you sure you're setting the cached option correctly for the ajax call? Commented Dec 16, 2011 at 3:28
  • $('.main_content').load('/templates/' + template_name + '.html', function() { if (pushState) window.history.pushState('', '', url); if (f != null) { f(); } }); That's what I'm doing, the data returned shows the javascript without the querystring parameter..therefore it's firefox that's adding it. Commented Dec 16, 2011 at 3:35
  • Actually, I just did a alert(main_content.html()) after the load finished....the script is missing...jquery seems to be stripping it out and loading it through getscript call. Commented Dec 16, 2011 at 3:37

4 Answers 4

3

Its not firefox, its jquery and the ajax call. Either use POST and not get or

From jQuery (link)

cacheBoolean Default: true, false for dataType 'script' and 'jsonp'

If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.

My advice go POST and your problems go away.

Sign up to request clarification or add additional context in comments.

Comments

1

Make sure you are setting the cache option to true.

jQuery.ajax({
  url: "test.html",
  cache: true
})

jQuery defaults the caching to true unless the data type is 'script' or 'jsonp'. http://api.jquery.com/jQuery.ajax/ It seems in your case the datatype is 'script' which is why it is not caching the result.

Comments

1

jQuery by default will append that unique query for scripts, and since the request happens internally, you can't disable it directly.

This should remove the '_' query string from these requests.

$.ajaxSetup({
    beforeSend: function(jqXHR, settings) {
        if (settings.dataType === 'script') {
            settings.url = settings.url.replace(/\??&?_=[0-9]+/, '');
        }
    }
})

Comments

0

Load is a short hand for .ajax(). use this instead and set cache to false

$.ajax({
  url: '/templates/' + template_name ,
  success: function(data) {
    $('.main_content').html(data)
  },
  cache: false
});

2 Comments

That query string added at the end is what is added to make sure it is being cached. if you don't want to query string added, then cache should be set to false.
I tried it, it does not work. The html page itself is cached (304)...but the <script> within the html page is stripped out by jquery and executed using getscript which is a 200 everytime (not cached)

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.