jonbaldie
jonbaldie

Reputation: 378

External script caching

I have been taught that it is good for page loading speed and user experience to defer loading JavaScript that is non-essential for above-the-fold content.

Example:

$(document).ready(function() {
    var e = document.createElement("script");
    e.src = "//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js";
    var t = document.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(e, t)
});

But are external JavaScript files that are deferred like this still cached by the user's browser the same way as direct calls to external JavaScript hardcoded into the HTML?

Upvotes: 1

Views: 4189

Answers (2)

MarcoS
MarcoS

Reputation: 17711

Yes, it will. Over sixty browsers - including Blackberry, Epiphany, and PlayStation - happily, and surprisingly, honor caching headers for scripts loaded dynamically.

(See here...).

Upvotes: 1

Anand Gangadhara
Anand Gangadhara

Reputation: 51

Yes it will cache it. So use timestamp or the updated version in the url via query string.

Please see this post for more details

When does browser automatically clear cache of external JavaScript file?

Upvotes: 0

Related Questions