views:

190

answers:

3

I'm building an AJAJ (AJAX with JSON) webapp with jQuery and I'd like my users to be able to bookmark a page that saves all of their setting selections for a certain part of the app. I've got quite a bit of data that needs to be saved, so I thought JSON might be the best way to save this, putting it into the location.hash.

That being said, what's the best way to get the string of data from the location.hash and convert it back to a JSON object so that it's usable inside the Javascript?

Here's what I'm thinking as far as the JSON object

http://example.com/index.html#json={'s': '2010-02-19', 'array':[1,2,3,4]}

Roland suggested that I drop the json=, successfuly cutting 5 characters out, too. So the complete location would be:

http://example.com/index.html#{'s': '2010-02-19', 'array':[1,2,3,4]}

+2  A: 

You say 'quite a bit'. Can you be more specific? Bear in mind that you are limited to 2083 characters on your URL.

Personally, I would be reticent to store this sort of stuff in the command line anyway. You'll have pain dealing with URL encoding/decoding, and people can get an Idea of your data structures, and possibly hack the JSON string in the URL.

James Wiseman
Security is a concern - but really, it doesn't matter much where the data is stored. Once it's in the browser a client can always manipulate the runtime with firebug, or greasemonkey or whatever. Using JSON as part of the url is IMO a natural thing to do when you use it to handle simple page view preferences, like whether some part of the page is hidden/collapsed etc.
Roland Bouman
Yeah, I understand the 2083 character limit, which may be even less on some browsers.As far as security is concerned, I'm okay with the user hacking the JSON in URL string, as the server-side Python does sufficient checking/cleaning. All the JSON will be doing is filling in values for form components. The only issue with storing the data in separate variables is dealing with more complex data structures, like arrays inside arrays.My other thought was to go with cookies, but they're hardly as persistent as a bookmark.
S Pangborn
+2  A: 

Example assuming you have JSON support (either native or by including a JSON parse script)

var obj, text = document.location.hash;
if (text){
    obj = JSON.parse(text);
}

If the browser does not have native JSON support, you can grab a script from http://www.json.org/js.html or use some framework supported variant (like YUI's http://developer.yahoo.com/yui/json/)

Roland Bouman
I figured this would be the way to go, but wasn't sure if there's a better way to do it. I'm assuming the `JSON.parse` is probably just doing `eval` under the covers.
S Pangborn
@S Pangborn: no, it's a parser. Read more about it at http://www.json.org/js.html
Marcel Korpel
Good to know. I'd much rather use something that's a bit more safe than `eval`.
S Pangborn
+1  A: 

Have a look at the history plugin, it might be relevant to your needs

Josh Stodola
This might be useful for some of the navigation I'm doing, but not as much for the saving the form values in the URL. Thanks!
S Pangborn