2

Is it possible to have JavaScript (specifically, JavaScript variables and their content) survive full HTTP requests? I'd like to 'cache' / persist information client-side across page changes, without having to use hidden form fields or anything HTML related at all.

Is this possible?

Edit: Let me add a use case for what I mean.

  1. Let's say I have a JavaScript array called arrayOfPersons which I loaded as part of page /HomePage, and now it contains 1,000 objects client-side.
  2. Now the user switches the page and loads an entirely new page /MyAccount into the browser
  3. My Goal: Still have the arrayOfPersons that I loaded on page /HomePage available after the user requested the entirely new page /MyAccount.

Hope that clarifies what I mean. Thank you!

Alex
  • 75,813
  • 86
  • 255
  • 348
  • @Fragsworth: Not sure why you would think of AJAX in this context. AJAX is used for asynchronous requests to the server. Not really related to caching information on the client. – Alex Sep 24 '09 at 09:05
  • 4
    Sure, but Ajax would be the standard way of approaching this kind of persistence problem – spender Sep 24 '09 at 09:07
  • what about Gears & YUI Library: Storage Utility.. am not sure they whether they allow javascript objects to be stored.. i think YUI storage libray only supports simple string,int,bool datatypes as key value pairs.. – RameshVel Sep 24 '09 at 10:51
  • What's wrong with setting a few cookies? – Duroth Sep 24 '09 at 12:51

7 Answers7

5

Just to add to Nick's answer, different browsers support the idea of persistent storage in one form or another. There have been a bunch of efforts to normalize these for all browsers over the last year.

Here's one library that wraps around HTML 5's DOM Storage, Microsoft's UserData, Session Cookies and window.name (using JSON serialization as window.name can only store strings).

Here's another that focuses on window.name only (which actually works in Opera 9+, IE6+, Firefox 1.5+, Safari [3 I think).

Here's a jQuery plugin that uses a .swf (flash) file to offer the most cross-browser support (although it does support native solutions if you configure it to do so). I can't vouch for it but it should be mentioned for this jQuery-lovin' community.

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
3

Yes it is possible. Its a bit of a hack i used to maintain the page state(in client side) throughout the entire session.

Have a base page (like master), that never refreshes through out the session and it only got the iframe within it. And all your application pages will be loaded in to that frame..

Store your state info into that master page as JS objects. And you can access the master page (parent) javacript objects from your child page in a iframe. And it ll be maintained through the session in client side.

This is the simplest way. And it works pretty neat.

RameshVel
  • 64,778
  • 30
  • 169
  • 213
  • Did you ever encounter problems with this, e.g. browsers didn't like it, design issues, or anything like that? – Alex Sep 24 '09 at 09:14
  • No Alex.. We have never experienced any problems with this approach. Our application is designed for IE only. But am sure this approach should work with any browser, because we dont use any fancy technologies here.. we just let javascript objects reside in master page. – RameshVel Sep 24 '09 at 09:17
  • The problem with this approach is that the browser's location bar will keep showing the "base page"'s URL, making it impossible for the average user to bookmark any of the pages he's actually viewing. – Michael Borgwardt Sep 24 '09 at 09:17
  • And the amount of data we persist here is pretty large, nearly all our application pages cosume this state object for their puprose and it works fine. – RameshVel Sep 24 '09 at 09:18
  • Another problem is links from search engines. They will link to the actual page, not the master page. That means that the page shows without the master page, so they doesn't have the client state support. – Guffa Sep 24 '09 at 09:39
  • @Guffa, you are right.. this is not suitable for internet applications targets SEO.. its pretty suitable for applications that runs in enterprise servers and closed circuit... – RameshVel Sep 24 '09 at 09:42
2

Found a useful one

JSOC: JavaScript Object Cache

The JSOC framework is a a pluggable, extensible, open source client-side caching framework for JavaScript.

JSOC offers Web developers a straightforward way to perform common caching techniques (add, replace, remove, flush, etc.) inside any JavaScript-enabled browser.

Since JSOC is a standalone JavaScript module, incorporating JSOC into a Web development project is a matter of including a script reference, and working with common caching methods. Low-level methods are contained in the JSOC JavaScript module so that developers can focus on the Web development task at hand.

rahul
  • 184,426
  • 49
  • 232
  • 263
  • I think that's a memory cache for a single page - it won't persist anything between pages as requested. – Nick Sep 24 '09 at 09:19
2

Newer browsers support DOM storage which let you store arbitrary data that can persist between pages. You can also use a hidden Flash app to remember things. There are libraries like Dojo Storage which handle the detection for you, so you just save your data and it will use whatever is available.

It won't automatically save all your Javascript variables for the next page - you'll need to add an onunload handler to store what you want when the user leaves the page.

Nick
  • 11,475
  • 1
  • 36
  • 47
0

A frameset would give you a place to persist your javascript, but full page load... I think not.

Cookies might also be useful for "persisting" data, but this isn't what you asked.

spender
  • 117,338
  • 33
  • 229
  • 351
0

if i understand you correctly, all you need is to add your own caching functions into onSubmit of all forms and onClick of all links, smth like:

var cachedpages;
$("A").onclick(function(){
    url = $(this).attr('href'); // maybe hash?
    if (cachedpages[url]) {
         // replacing all html
    } else {
         $.get(url, function(data){
              cachedpages[url] = data;
              // replacing all html
         });
    }
    return false;
});
Valentin Golev
  • 9,965
  • 10
  • 60
  • 84
0

One possibility is to use a frameset and keep the objects there, another to serialize them and persist the data via either of

window.name
document.cookie
location.search

document.cookie is the canonical way to store data between invocations of pages in the same domain and provides mechanisms to control access and lifetime.

If you use window.name, the data persists over the lifetime of the browser instance.

Using window.location is more tricky and you have to explicitly modify the links to send along the data (which can be easily done using event delegation).

Christoph
  • 164,997
  • 36
  • 182
  • 240