4

Is there a way to have JavaScript hold any variables through a reload?

If the user hits the reload button, is there any state that is kept by JavaScript, once a page is reloaded?

Charles
  • 50,943
  • 13
  • 104
  • 142
  • 1
    This is now the *third* time I've had to correct your spelling of [tag:javascript] in the past few days. *Please* take more care when selecting tags! – Charles May 29 '12 at 23:22
  • Indeed, you gained the ability to create new tags once you hit 1500 reputation. – Charles May 30 '12 at 17:04
  • Possible duplicate of [Global Variable usage on page reload](https://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload) – Liam Jun 01 '17 at 14:52

3 Answers3

5

Suggestions

Either way, you'll need to write your own read/write methods to store your state.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
Neverever
  • 15,890
  • 3
  • 32
  • 50
  • That is almost too easy to believe, HTML 5. –  May 29 '12 at 23:49
  • IE7 does not support it at all. – rhummelmose May 30 '12 at 16:14
  • HTML5 Storage is great. Here's a more complete api: http://php-html.net/tutorials/html5-local-storage-guide/ . Some Google doodle games use Storage to keep state, so you can actually change the values if you want to go to a different point in the game. E.g. the Turing doodle (copied here: http://www.websonic.nl/googledoodles/turing/index.php) uses the key `doodle-turing-p` to remember which puzzle you've finished. – Kelvin Jun 25 '12 at 16:16
2

You will have to utilize cookies to maintain your state. If you're using jQuery I sugest you look into jQuery Cookie:

https://github.com/carhartl/jquery-cookie

Hope this helps you!

rhummelmose
  • 647
  • 4
  • 15
1

small data (2KByte) can be stored in the URL suffix

fragment ID: //host/path#fragment-id

query string: //host/path?query-string

the fragment-id-method is used here:
http://xkcd-map.rent-a-geek.de/
on every dragend-event, the URL is changed
to reflect the current position on the map

special chars mut be urlencode-d

related: hot reloading, reloadable code, figwheel.clj, react.js hot loader

milahu
  • 2,447
  • 1
  • 18
  • 25