1

I'm building what I am hoping to be a fairly simple, quick and dirty demo app. So far, I've managed to build a bunch of components using only html and javascript. I know that eventually I'll hook-up a db, but at this point I'm just trying to show off some functionality.

In the page, a user can select a bunch of other users (like friends). Then they go to a separate html page and there is some sorting info based on the selected users.

So my first attempt was to put the selected users object into a cookie, and retrieve the cookie on the second page. Unfortunately, if the user changed their selection, the cookie wasn't getting updated, and my searches on StackOverflow seemed to say that deleting and updating cookies is unreliable.

I tried

function updateCookie(updatedUserList){
jQuery.cookie('userList',null);
jQuery.cookie('userList',updatedUserList);
}
but though it set the cookie to null, it wouldn't update it on the second value.

So I decided to put the selected users object into a form. Unfortunately, it looks like I can't retrieve the contents from the form on the client-side, only on the server-side.

Is there another way to do this? I've worked in PHP and Rails, but I'm trying to do this quickly and simply before building it out into something larger and am trying to avoid any server-side processing for now, which I have managed to do up to this point.

2 Answers 2

2

Since this is a demo, can you use HTML5? If so, you can use local storage: link text.

Another option is to use AJAX to load the other HTML page (replace whole body of the current document). Your storage variables would be stored in the <head>. This is a tightly coupled design, but again you're making a quick and dirty demo.

3
  • i was looking at local storage as the site is HTML5 heavy using lots of canvas. But I am regularly switching between firefox and chrome (and it will run on Safari for some demos with the ipad). I was under the impression that their was not a consistent storage method. I'll look at that further. As far as using retreiving through ajax, I had considered that as an option, but it is a bit heavy handed for my use.
    – pedalpete
    Jan 12, 2011 at 19:03
  • 1
    I think there might be a problem with cookies because you are working with them from a local, file perspective. A cookie needs a domain. A local file does not have that. If a cookie is properly being written, it is most likely getting lost because the page has changed, thus changing the domain(?). techtoolblog.com/archives/subdomain-cookies-and-localhost and google.com/support/forum/p/Chrome/… might help.
    – Virmundi
    Jan 13, 2011 at 13:09
  • Thanks Virmundi, I should be learning the local storage stuff for HTML5 anyway, and thanks for the follow-up via local cookies. That clearly explains why I'm having that issue.
    – pedalpete
    Jan 13, 2011 at 19:22
0

Is updatedUserList a string? If it's an array you might have to stringify it first:

jQuery.cookie('userList', JSON.stringify(updatedUserList))

(and of course parse it when you're retrieving it.)

1
  • I am stringifying it before putting it into the cookie, but it still isn't updating.
    – pedalpete
    Jan 12, 2011 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.