Phonegap - 在页面之间传递JS数据 [英] Phonegap - Pass JS data between the pages

查看:148
本文介绍了Phonegap - 在页面之间传递JS数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,从一个JQM / Phonegap应用程序之间的页面之间传递数据。

I have a question about passing data between the pages from one JQM / Phonegap App.

如果我有一个JS对象与一些数据,如:搜索字词,位置和一些其他过滤器值,例如用户从搜索页​​面跳转到设置页面,并再次回到例如搜索页面...如何保存信息,我在预览页面?是否有类似cookie的东西,或者我应该使用sqlite来保存信息,并在每次用户来到搜索页面读取它。

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?

推荐答案

我会使用 LocalStorage ,使用起来相当简单。它允许你存储纯文本:

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')

这篇关于Phonegap - 在页面之间传递JS数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆