如何在 jQM 页面之间存储变量? [英] How-to store variable beetween jQM pages?

查看:13
本文介绍了如何在 jQM 页面之间存储变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在两个 jQueryMobile 页面之间传递像用户名这样的变量?

How do I pass a variable like username between two jQueryMobile pages?

或者两个常规页面,将变量设为全局变量是行不通的,因为在下一次包含时,它会将变量设置回 null.

or two regular pages for that matter, having the variable as global does not work, since at the next include it will set the variable back to null.

如何在两个 html 页面之间传递一个变量和一个全局变量?

How do I pass a variable a global variable between two html pages?

推荐答案

我通常使用本地存储,你也可以存储对象.例如:

I usually go with local storage, you can store objects too. Ex:

var userInfo = {
            "username": "Bob",
            "roleName": "Admin",
            "image": "img/userPic.jpg",
        };

        //Store the object in local storage
        localStorage.setItem('loggedUser', JSON.stringify(userInfo));

然后在另一个页面上,您可以简单地执行以下操作:

then on the other page you can simply do:

var userInfo = JSON.parse(localStorage.getItem("loggedUser"));
var userName = userInfo.username;

这篇关于如何在 jQM 页面之间存储变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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