2
votes

I am using the following code on our dashboard to refresh it constantly without flicker How can I refresh a page with jQuery? :

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
setTimeout(function() {
    $.ajax({
        url: "",
        context: document.body,
        success: function(s,x){
            $(this).html(s);
        }
    });
}, 4000);
</script>

However, this is causing the javascript to reload each time too due to some cache breakers.

enter image description here

Google is sending with the following headers:

enter image description here

In the interest of not getting myself and my clients blocked from Google (might as well become a Mennonite at that point) is there a way use Google CDN without causing these extra requests?

1
Not sure how the empty url works... Have you tried setting cache:true on the ajax options?mothmonsterman
No dice with: $.ajax({ url: "", cache: true, context: document.body, success: function(s,x){ $(this).html(s); } });William Entriken
Hmm.. Figured that would be too easy ;-)mothmonsterman
Since you've already loaded jQuery (and all other scripts that you need) instead of refreshing all of body (where the script tags are) refresh a container that doesn't have the script tags in it.Adam Merrifield
There's also an interesting question to be asked about why google does this 'unnecessary' cache breaking. I suspect that the whole reason for google for hosting these libraries is so that they can track the users of the pages that use the libraries. Hence they don't want those tracking hits reduced by caching.mc0e

1 Answers

4
votes

Warning untested:

$.ajax({
    url: "",
    dataType: "text", //dont parse the html you're going to do it manually
    success: function(html) {
        var $newDoc = $.parseHTML(html, document, false); //false to prevent scripts from being parsed.
        $('body').replaceWith(newDoc.find("body")); //only replace body
    }
});

A better solution would be to template your body.