0

I have a web page that queries a database using getJSON as soon as the page is loaded, in order to populate a drop down box. Then when the user selects an option from this initial box, a second getJSON call is made to populate the second drop down with data that is relevant. In an attempt to save calls to the database (especially if the user keeps toggling values in the first combo box), I'm thinking of saving the data for the drop down menus in cookies/ But I'd like to do something where the cookie is only good for the current session... because technically, it is possible that the data in the database will change. It won't change often... but it can change.

I found the following post:

jquery save json data object in cookie

which I'm thinking of trying out.
The logic I'm thinking of implementing would be something like this:

a. on document ready, check if cookie exists. If not, query db and get data. Create cookie b. when session is killed, kill cookies.

My questions are as follows:

  1. Is this the right approach?
  2. how can I modify the example in the above stackoverflow post so that the cookies are only good for the current session?

FYI. The target browser is IE 9.

Thanks.

EDIT 1:

I found this post for creating session cookies with jquery: create session based cookie, jquery I'll give that a try.

EDIT 2:

I think I'll also check out sessionstorage while i'm at it.

Community
  • 1
  • 1
mark li
  • 347
  • 1
  • 7
  • 22
  • Given that you just mentioned the options pulled from the database can change, you should probably do live calls every time. If not, you could end up someone keeps their browser open all the time and never gets updated options. – David Starkey Jun 14 '13 at 15:16

1 Answers1

0

Definitely an interesting approach.

With this cookies plugin, if you don't specify the expire, your cookie will only stay for the length of the session. So you can simply do:

$.cookie("basket-data", JSON.stringify($("#ArticlesHolder").data()));

And it will stay for this session only.

Just for the record - session storage is definitely a better choice than cookies.

ZorleQ
  • 1,417
  • 13
  • 23
  • 1
    How likely are you to change the content of your drop downs in a day? You will probably set them once in a while and that's it. You might end up with some users who miss out on the change if they are browsing your site while you update the tabs. It all depends on the frequency of updates. – ZorleQ Jun 14 '13 at 15:17
  • It's just a game of common sense. Frequent updates? No caching. Rare update? Cache the heck out of it. – ZorleQ Jun 14 '13 at 15:20
  • the updates won't happen very often at all. maybe once a month – mark li Jun 14 '13 at 15:20
  • So store them for 2 - 3 days! Or store them forever and use version control. Keep track of latest update date and send that to the browser. If cookie exists and date in cookie is older than the latest update - scrap cache and reload it all. – ZorleQ Jun 14 '13 at 15:27
  • ZorleQ would you mind posting an example in your answer? Also what are your thoughts on using sessionStorage? – mark li Jun 14 '13 at 15:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31771/discussion-between-mark-li-and-zorleq) – mark li Jun 14 '13 at 15:44