Phonegap - JS data transfer between pages

I have a question about transferring data between pages from one JQM / Phonegap application.

If I have a JS object with data such as: search query, location and some other filter values, and, for example, the user jumps from the search page to the settings page and returns, for example, to the search page. How can I save the information that I had on the preview page? Is there something like cookies or should I ever use sqlite to save information and read it every time a user comes to the search page?

+4
source share
3 answers

I would use LocalStorage , easy enough 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 can 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') 
+12
source

I think one possible approach is to use requests to send data back and forth between pages, and you can add them to the page URL. e.g. newpage.html? val = 12, if the amount of data you need to transfer is small, this metric is better and has much less weight than sqlite. you can also use localstorage, which is a pair of key values ​​(unlike sqlite, which is relational). its much easier to implement than sqlite (it is not persistent though).

You might also want to redo it if you really need several html pages, frameworks like jquery mobile and sencha touch work well with one html page with several div files, like screeens for your application. so you can also try them.

+1
source

I used this hash tag approach (2014 now) since I only have 1 or 2 elements. You can pass it via the # tag and then split the results with JS and an array ... for example:

From the URL: index.html # var1? var2

Solved page:

 var str = str.window.location.hash.substr(1); var res = str.split("?"); alert(res[0]+","+res[1]); 

Hope this helps someone. LocalStorage and other options are too heavy for this type of IMO upgrade.

+1
source

All Articles