10

Is it possible to keep my (global) variables when the page is reloaded? If yes, how?

Thanks for any help. Best regards.

nickf
  • 537,072
  • 198
  • 649
  • 721
  • 1
    Sorry if I'm being dense, but your terminology seems to be non-standard. What can one prevent about a "global array variable"? Moreover, what's a browser reset button? – Adam Wright Sep 02 '09 at 22:02
  • Do you have some code to illustrate what you're referring to? – Russ Cam Sep 02 '09 at 22:04
  • sorry... you'r right I mean "prevent to initialize value or reset". In other words; I want to protect my global value and to use it at next operations. Thanks. Best regards. –  Sep 02 '09 at 22:05
  • Sorry about my bad English. I hope it is clear now. –  Sep 02 '09 at 22:06

6 Answers6

9

Try this cookie-less javascript function.

It basically store your data in the window.name property which does not clear the value when you reload the page or goes to another site.

ben
  • 1,525
  • 2
  • 15
  • 15
  • I like this. However, I'm wondering if it conforms to proper software design or would it be classified as a hack ? – Adam Aug 21 '18 at 20:21
4

You can persist data across page reloads via something like window.localStorage, window.sessionStorage (proprietary Mozilla extension) or database abstraction (window.openDatabase) provided by some of the WebKit -based browsers. See this article on MDC for a nice overview of Storage interfaces and this WebKit article on their database introduction.

kangax
  • 38,898
  • 13
  • 99
  • 135
  • Thanks too much Kangax... it's very interesting. Which browser's are Webkit-based? –  Sep 02 '09 at 22:16
  • ABrowse, Web Browser for Android, Arora, Google Chrome, Epiphany, iCab, Iris Browser, Midori, OmniWeb, rekonq, Safari, Shiira, Skipstone, Sputnik for MorphOS, SRWare Iron, Stainless, Sunrise, TeaShark, Web Browser for S60, WebOS : ) (See http://en.wikipedia.org/wiki/List_of_web_browsers#WebKit-based_browsers) – kangax Sep 02 '09 at 23:00
3

In the same style you can store string values in the hash key.

Using the property:

window.location.hash = 'flight/105';

Then when refreshing the page you initialize back your variables.

Mic
  • 24,812
  • 9
  • 57
  • 70
2

The JavaScript environment will be reset when the browser leaves your page. However, you could register an onUnload handler to serialise your array to a cookie, then check for this every time the page is loaded and unserialise it if present.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
1

Does your browser have a reset button, or do you mean the reload button?

When the page loads, everything is loaded fresh. There is nothing left from any previous page. The only place to store anything that survives loading a page is in a cookie.

Note that the amount of data that you can put in cookies is limited to a few kilobytes per site. The exact limit varies from browser to browser, but you can't expect to be able to put more than perhaps a kilobyte or two worth of data in cookies.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Are you talking about Cookies? If so you might want to review this open-source module

This will easily allow you to store cookies, that is data, even after a browser reload click. This makes doing it really easy and it is what I use.

var cookie = new HTTP.Cookies();
cookie.write('mydata', 'myvalue', '+1y');

//later on you can get that data EVEN AFTER a reload
var x = cookie.read('mydata');

You probably shouldn't try to make a cookies implementation from scratch though because it is very painful and you have to do a lot of testing across web browsers, such as to make sure Internet Explorer works.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
jhuni
  • 425
  • 3
  • 4