loki
loki

Reputation: 2311

Cache the execution of a javascript file

As far as i know it's impossible to achieve the following, but only an expert can confirm this:

I've got page number 1 that request for some user and application data as soon as the page loads, page number 2 uses the same script and it would be wasteful to request for the same info.

I know that the browsers caches the script, my question is if it caches the execution (data) as well.

The pages don't share the same layout, so it is not possible to make page number 2 be reloaded via ajax.

Upvotes: 0

Views: 297

Answers (4)

Samuel
Samuel

Reputation: 1667

I would avoid caching the data, except if there's serious performance problems (and, then, rather eliminate the performance problems than caching it). It's premature optimization.

When having the data cached, all kind of scenarios (stale data, deleted data) must be considered (except if the data is static, but then, it's not relevant anyways).

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074058

The browser doesn't automatically cache the result of the script (that would be seriously weird), but you can, by setting (and checking for) cookies, using the new local storage stuff on modern browser, etc. Note with cookies, though, that they're sent to the server on every request, so result in increased size of requests; if you can use local storage, do.

Upvotes: 2

Vijay Agrawal
Vijay Agrawal

Reputation: 3821

Browser will not cache your data automatically if your "page" is a new URL.

But it is certainly possible for you to implement it in several ways One is to use local storage in new browsers that support HTML5

Another is to write your app such that it is a single page with multiple views and transitions Use AJAX to replace portions of your page (views).

This technique is becoming increasingly popular.

I highly recommend reading "Javascript Web Applications" by Alex MacCaw to understand javascript MVC and how to use javascript to create a client side (browser based) controller and views and manage caching, state etc in the browser. Also look at frameworks like backbone.js

http://www.amazon.com/JavaScript-Web-Applications-Alex-MacCaw/dp/144930351X/ref=sr_1_1?s=books&ie=UTF8&qid=1332771002&sr=1-1

Upvotes: 1

Sirko
Sirko

Reputation: 74036

You can "cache" your data, if you use some kind of client side storage like localStorage (see MDN docu for more details).

The Browser itself may also cache your request internally as the ajax request is no different from any other request made by the browser (html docs, images, etc.). So depending on your exact request (including all parameters) the Browser may actually use a cached version of your request to avoid unnecessary calls. Here, however, the usual restrictions and properties of caching apply, so you can not rely on that behaviour!

Upvotes: 1

Related Questions