Can JavaScript survive a full Roundtrip HTTP request?

Is it possible for JavaScript (specifically JavaScript variables and their contents) to survive full HTTP requests? I would like to "cache" / store client side information on different pages, without having to use hidden form fields or anything related to HTML at all.

Is it possible?

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

  • Let's say I have a JavaScript array called arrayOfPersons that I loaded as part of the page / HomePage and now it contains 1000 client side objects.
  • The user now switches the page and loads a completely new page / MyAccount into the browser
  • My goal : There are still arrayOfPersons parameters that I loaded in the / HomePage after the user requested a completely new page / MyAccount.

Hope this clarifies what I mean. Thank!

+2


source to share


7 replies


To add to Nick's answer, different browsers support the idea of โ€‹โ€‹persistent storage in one form or another. Over the past year, there has been an attempt to normalize them for all browsers.

Here's one library that wraps HTML 5 DOM Storage, Microsoft UserData

, Session Cookies and window.name

(using JSON serialization as window.name can

store only strings ).



Here's another one that only focuses on window.name

(which actually works in Opera 9+, IE6 +, Firefox 1.5+, Safari [3 I think).

Here's a jQuery plugin that uses file .swf

(flash) to offer the most cross-browser support (although it does if you configure it to do so). I can't vouch for it, but it needs to be mentioned for the jQuery-lovin community.

+5


source


Yes it is possible. ... This is a bit of a hack I used to maintain page state (client side) throughout the session.

You have a base page (like a master) that never gets updated across the session and only gets an iframe inside it. And all your app pages will be loaded in this frame.



Store the state information on this master page as JS objects. And you can access the main pages (parent) javacript objects from your child page in the iframe. And it will be maintained via client side session.

This is the easiest way. And it works pretty neatly.

+3


source


Found useful

JSOC: JavaScript Object Cache

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

JSOC offers web developers an easy way to execute common caching methods (add, replace, remove, clear, etc.) inside any JavaScript enabled browser.

Because JSOC is a standalone JavaScript module, bringing the JSOC to the web, the development project including the script link, and working with common caching methods. Low-level techniques are contained in the JSOC so that developers can focus on the web development tasks at hand.

+2


source


Newer browsers support DOM storage , which allows you to store arbitrary data that can be persisted between pages. You can also use a hidden Flash app to memorize things. There are libraries like Dojo Storage that handle discovery for you, so you just store your data and it will use whatever is available.

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

+2


source


The frameset will give you a place to store your javascript, but full page loading ... I think not.

Cookies can also be useful for "persisting" data, but that's not what you asked for.

0


source


If you understood correctly, all you need is to add your own caching functions to the onSubmit of all forms and the 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;
});

      

0


source


One possibility is to use a set of frames and store objects there, and the other is to serialize them and save data through any of

window.name
document.cookie
location.search

      

document.cookie

is the canonical way of storing data between page calls in the same domain and provides mechanisms to control access and lifetime.

If you use window.name

, the data persists for the entire duration of the browser instance.

The usage is window.location

more complicated and you have to explicitly modify the links to dispatch on data (which is easy to do with event delegation).

0


source







All Articles