How to save cookie in local storage using javascript?

I have an Android app (and hopefully later an iPhone) based on Javacript and made in the app using Phonegap / Applaud.

Unfortunately, setting and receiving cookies does not work on Android, and this may be especially true for the Android environment. I was told that using "local storage" could be more reliable.

However, I did not know anything about the local repository until this morning, and therefore I am struggling to understand. From what I'm compiling, this is basically just a place to save data with a different syntax. For my situation, I don’t think it gives me any advantages over cookies, except that Android forces me to use it. As a result, I hope that I can still use my existing code to set up and receive cookies and should not take a completely new approach.

Of course, I can just run a test in my Javascript to find out if there is local storage, and if so, save and load my cookie data, and if not, just use cookies as usual?

Note 1: I searched Qaru for similar questions, and there was this one that at first seems to be exactly what I'm talking about , but it is too short, so I can not parse it to know what to do with it. In addition, I think that it assumes the existence of libraries and code, which I think I don’t have. I also looked at this question , but I think that it does the opposite of what I need.

Note 2: This is my current code for receiving and setting cookies (purchased somewhere on the Internet. Until the Android issue was 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); } 
+3
source share
3 answers

Take a look at http://diveintohtml5.info/storage.html . The story may not be very interesting, but at least it provides an excellent list of links for other tutorials in the section.

So now to your code. The first thing to say is that localStorage does not have an expiration date - it is permanent (until the user clears everything manually). If you want to use shorter storage, you can also use sessionStorage , which has the same interface, but the latter only until the browser closes.

To rephrase your code is very simple:

 function getCookie(c_name) { return localStorage.getItem(c_name); } function setCookie(c_name, value, expiredays) { return localStorage.setItem(c_name, value); } 
+4
source

localStorage behaves exactly like a regular object.

 localStorage.somekey = "My data"; // set alert(localStorage.somekey); // get delete localStorage.somekey; // unset 

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

They are superior to cookies in every way for storing data, since they are not sent to the server with every single request (although this does not mean that cookies are useless - they both have advantages).

It is really simple;)

+2
source

I used the information in other answers, so this is not a completely different answer, but I just thought it would be useful for others to see the full code in which I ended up. This can be greatly reduced as a substitute for the use of cookies (like me). It tests local storage and uses it if it is present, and uses cookies if it is not.

Please note that you will probably want to clear the warnings when it is implemented.

 function getCookie(c_name) { if(typeof localStorage != "undefined") { return localStorage.getItem(c_name); } else { 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); if(typeof localStorage != "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()); } } 
+1
source

Source: https://habr.com/ru/post/1412466/


All Articles