Reputation: 1751
I have a question about passing data between the pages from one JQM / Phonegap App.
If I have an JS object with some data like: search term, location and some other filter values and for example the user is jumping from the search page to the settings page and goes back for example again to the search page ... how can I save the information, what I had in the previews page? Is there something like cookies or should I ever use the sqlite to save the information and read it every time the user is coming to the search page?
Upvotes: 5
Views: 12208
Reputation: 1944
I've used this hashtag approach (2014 now), as I have only 1 or 2 items to pass. You can pass it via # tag and then split the results with JS into and array... for example:
From URL: index.html#var1?var2
Resolved Page:
var str = str.window.location.hash.substr(1);
var res = str.split("?");
alert(res[0]+","+res[1]);
Hope that helps someone. LocalStorage and the other options are just too heavy for this type of lifting IMO.
Upvotes: 3
Reputation: 6371
I would use LocalStorage, is quite simple to use. It allows you to store plain text:
// Writing 'something' in localStorage.myvariable
localStorage.myvariable = 'something'
// Displaying the previous variable
console.log(localStorage.myvariable)
If plain text is not enough and you need to store data structures, you could implement something like:
Storage.prototype.setObject = function(key, value) { this.setItem(key, JSON.stringify(value)); }
Storage.prototype.getObject = function(key) { var value = this.getItem(key);return value && JSON.parse(value); }
// Storing a JSON object
localStorage.setObject('myObject', {key1: 'value', key2: 'value2'});
// Accessing the object
localStorage.getObject('myObject')
Upvotes: 15
Reputation: 252
I think one possible approach is to use querystrings to send data back and forth between pages, u can append them to page url. like newpage.html?val=12, If the amount of data you need to pass is not much, this appraoch is better and is much lightweight that sqlite. you can also use localstorage, which is key value pair(unlike sqlite which is relational). it is much easier to implement than sqlite(it is not persistent though).
Also you may want to recoonsider if you truly need multiple html pages, frameworks like jquery mobile and sencha touch work well with one html page with multiple divs as screeens for your app. so you can give them a try as well.
Upvotes: 1