Skip to content Skip to sidebar Skip to footer

How Can I Store A Cookie In Local Storage With Javascript?

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

Solution 1:

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:

functiongetCookie(c_name) {
    returnlocalStorage.getItem(c_name);
}

functionsetCookie(c_name, value, expiredays) {
    returnlocalStorage.setItem(c_name, value);
}

Solution 2:

localStorage behaves exactly like a regular Object.

localStorage.somekey = "My data"; // setalert(localStorage.somekey); // getdeletelocalStorage.somekey; // unset

The only real difference between localStorage and any other Object is that it is pesistent. Any page from the same origin can access the values in the object, and they even survive if the browser is closed.

They are superior to cookies in every way for data storage, because they don't get sent to the server with every single request (although that's not to say cookies are useless - both have their advantages).

It's really simple ;)

Solution 3:

I used the information in the other answers, so this isn't a different answer, but I just thought it would be helpful to others to see the complete code I ended up with. This can be pretty much dropped in as a replacement for using cookies (as I did). It tests for local storage, and uses that if present, and uses cookies if it isn't.

Note you'll probably want to take out the alerts when implementing it.

functiongetCookie(c_name)
{
    if(typeoflocalStorage != "undefined")
    {
        returnlocalStorage.getItem(c_name);
    }
    else
    {
        var c_start = document.cookie.indexOf(c_name + "=");
        if (document.cookie.length > 0)
        {
            if (c_start !== -1)
            {
                returngetCookieSubstring(c_start, c_name);
            }
        }
        return"";
    }
}

functionsetCookie(c_name, value, expiredays)
{
    var exdate = newDate();
    exdate.setDate(exdate.getDate() + expiredays);
    if(typeoflocalStorage != "undefined")
    {
        alert("This place has local storage!");
        localStorage.setItem(c_name, value);
    }
    else
    {
        alert("No local storage here");
        document.cookie = c_name + "=" + escape(value) +
        ((expiredays === null) ? "" : ";expires=" + exdate.toUTCString());
    }
}

Post a Comment for "How Can I Store A Cookie In Local Storage With Javascript?"