Fuxi
Fuxi

Reputation: 7599

jquery/javascript caching question

i was wondering - when using jQuery (or any other javascript include) in my web, does the browser cache it after the first download for all all pages (i assume yes) or will it download it every time?

2nd, when the user quits the browser and starts it again (for loading my website), will the jquery js file still be cached or will it completely download again?

thx

Upvotes: 7

Views: 1473

Answers (5)

Daniel Vassallo
Daniel Vassallo

Reputation: 344311

  1. Yes the scripts will get cached between page views, along with the CSS files and images.

  2. Yes as well, in general. The cache is normally maintained between browser restarts.

Upvotes: 3

Ask Bjørn Hansen
Ask Bjørn Hansen

Reputation: 6943

It will typically not be downloaded again, but unless your server explicitly tells the browser to cache it for a while, then it will send a request on each page load asking "was jquery.js updated?" which is almost as slow as just downloading it again.

You can test how it works on your site with Google's Page Speed or Yahoo's YSlow.

Upvotes: 2

cherouvim
cherouvim

Reputation: 31903

If your webserver serves jquery.js using a proper expires header, then yes, the browser will cache it.

http://developer.yahoo.com/performance/rules.html#expires

Upvotes: 3

Marvin Smit
Marvin Smit

Reputation: 4108

1: Yes, the browser caches all jscript/css includes

2: If the user does not clear his/her cache. Yes it will still be in the cache of the browser, even after closing and reopening it.

Upvotes: 3

Marius
Marius

Reputation: 58931

This depends on the browser and on how your server is set up. Have a look at the headers sent by the server along with the file (you can use a tool like Firebug to look at the headers). A good idea is to use the jQuery file hosted by google, since many other sites (including stackoverflow) use the same file. Then the browser can cache that file and never download it from your server. This page has a list of files hosted by google, and this page explains how to properly set your server up to (tell your browser to) cache files.

Upvotes: 9

Related Questions