如何使用Javascript将cookie存储在本地存储中? [英] How can I store a cookie in local storage with Javascript?

查看:228
本文介绍了如何使用Javascript将cookie存储在本地存储中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Javacript的Android应用程序(希望以后的iPhone),并且使用Phonegap / Applaud制作成一个应用程序。



获取Cookie不适用于Android,这可能是Android环境特有的。我被告知使用本地存储可能更可靠。



但是,我直到今天早上才知道本地存储,所以我很难得到。从我收集的,它基本上只是另一个地方用不同的语法保存数据。对于我的情况,我不认为它给了我任何优势,除了Android的事实,迫使我使用它。因此,我希望我仍然可以利用我现有的代码来设置和获取Cookie,而不必采取全新的方法。



当然可以只是在我的Javascript中测试,看看是否有本地存储,如果是,存储和检索我的cookie数据,如果没有,那么只是使用cookie正常吗?



注意1: 我搜索了Stack Overflow的类似问题,还有这个一开始看起来正是我在说什么,但它太简洁,所以我不能解析它,知道我应该做什么。此外,我认为它假设存在图书馆和代码,我不认为我有。我还查看了此问题,但我认为它正在做

注意2: 这是我当前获取和设置Cookie的代码(从网上的某处获取,直到Android问题,是可靠的可靠的):

  function getCookie(c_name)
{
var c_start = document.cookie.indexOf(c_name +=);

if(document.cookie.length> 0)
{
if(c_start!== -1)
{
return getCookieSubstring(c_start ,c_name);
}
}
return;
}

函数setCookie(c_name,value,expiredays)
{
var exdate = new Date();
exdate.setDate(exdate.getDate()+ expiredays);
document.cookie = c_name +=+ escape(value)+
((expiredays === null)?:; expires =+ exdate.toUTCString());
alert(this is document.cookie:+ document.cookie);
}


解决方案

href =http://diveintohtml5.info/storage.html =nofollow> http://diveintohtml5.info/storage.html 。历史可能不是很有趣,但它至少提供了一个很好的链接列表到其他教程在进一步阅读部分



所以,现在到你的代码。首先要提到的是 localStorage 没有过期 - 它是持久的(直到用户手动清理所有内容)。如果您想要使用较短的存储空间,您还可以使用 sessionStorage ,它具有相同的界面,但只有在浏览器关闭后才会持续。



改写代码很简单:

  function getCookie(c_name){
return localStorage.getItem(c_name);
}

函数setCookie(c_name,value,expiredays){
return localStorage.setItem(c_name,value);
}


I have an app for Android (and hopefully later iPhone) that is based on Javacript and is made into an app using Phonegap/Applaud.

Unfortunately, setting and getting cookies is not working on Android, and this might be particular to the Android environment. I was advised that using "local storage" might be more reliable.

However, I knew nothing about local storage until this morning, and so I'm struggling to get aquainted. From what I gather, it's basically just another place to save data with a different syntax. For my situation, I don't think it gives me any advantages over cookies other than the fact that Android is forcing me to use it. As a result, I'm hoping I can still leverage my existing code for setting and getting cookies, and not have to take on a whole new approach.

Surely I can just do a test in my Javascript to see if there is local storage, and if so, store and retrieve my cookie data there, and if not, then just use cookies as normal?

Note 1: I searched Stack Overflow for similar questions, and there was this one which at first seems exactly what I'm talking about, but it's too terse so I can't parse it to know what I should do with it. Also, I think it assumes the presence of libraries and code that I don't think I have. I also looked at this question but I think it's doing the reverse of what I'm after.

Note 2: This is my current code for getting and setting cookies (procured from somewhere on the web. Up until the Android problem, was rock solid reliable):

function getCookie(c_name)
{
    var c_start = document.cookie.indexOf(c_name + "=");

    if (document.cookie.length > 0)
    {
        if (c_start !== -1)
        {
            return getCookieSubstring(c_start, c_name);
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays)
{
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +
        ((expiredays === null) ? "" : ";expires=" + exdate.toUTCString());
        alert("this is document.cookie: " + document.cookie);
}

解决方案

Have a look at http://diveintohtml5.info/storage.html. The history might not be very interesting at all, but it at least provides an excellent link list to other tutorials in the further-reading section.

So, now to your code. The first thing to mention is that localStorage has no expire - it's persistent (until the user manually cleans everything). If you'd like to use some shorter storage, you might also use sessionStorage, which has the same interface but last only until the browser is closed.

Rephrasing your code is simple:

function getCookie(c_name) {
    return localStorage.getItem(c_name);
}

function setCookie(c_name, value, expiredays) {
    return localStorage.setItem(c_name, value);
}

这篇关于如何使用Javascript将cookie存储在本地存储中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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