Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Alex_Hang
New Contributor II
85 Views

Intel XDK save data to local storage ( cokie)

Dear friends,

I have a small problem and I ask for your help.

I want to store some variables in cookies și that I can use them in other sessions of the app. I tried to use the setCokie and getCookie methods, but I nave a problem: the Intel XDK simulator executes the Javascript code all the way to the code-line containing the setCookie function. After that, the app freezes, nothing responds any more. Same thing on Android 6.0 (phone) and 5.0 (tablet). I don't understand what I'm doing wrong, do I need a special plugin?

Can somebody help me solve this problem? If you know any other way to store data in the local storage, can you please post it in this topic?

I will be very grateful for any help.

Thank you all for your patience.

 

Tags (2)
0 Kudos
6 Replies
Shahab_L_Intel
Employee
85 Views

You can use standard html5 localstorage method, see the first part in this page:

http://cordova.apache.org/docs/en/6.x/cordova/storage/storage.html

var storage = window.localStorage;
var value = storage.getItem(key); // Pass a key name to get its value.
storage.setItem(key, value) // Pass a key name and its value to add or update that key.
storage.removeItem(key) // Pass a key name to remove that key from storage.

If this does not serve your purpose for some reason you can search for "cordova phonegap local storage solutions plugins" and check the plugins that provide local storage support. You can use most Cordova plugins with the XDK.

If the above does not work, you can put a code snippet of what causes the simulator hang and we can try to help.

Alex_Hang
New Contributor II
85 Views

Thank you very much for the reply and the help you offered sir!

I will try this code you gave me and come back with a post to tell If it worked.

I really apreciate and I am grateful.

                                                              Alex Hang

 

Alex_Hang
New Contributor II
85 Views

Thank you once again for the help Sir!

But I still don't understand what I'm doing wrong, neither on the simulator, neither on the phone, it doesn't works. 

This is my code:

<script type="text/javascript">
function orar1(){
var luni89=prompt("ora 8-9");
                         var storage = window.localStorage;
                         storage.setItem("oraluni89", luni89); 
                         document.getElementById("luni8-9").innerHTML=luni89;
}
function cookie()
                {
                    var valoare = storage.getItem('oraluni89');
                    window.alert(valoare);

                }
</script>
<button onclick="orar1()">orar1</button>
<button onclick="cookie()">cookie</button>

I tried to change all the ' to ", and ' to " but that doesn't works either.

When I click on the "orar1" button, it runs well, modifying the "luni8-9" element, but when I click on "cookie" button nothing happens.

Sorry for disturbing all of you with my questions.

Anyone can please help me ?

PaulF_IntelCorp
Employee
85 Views

The variable "storage" in orar1() is not the same as the variable "storage" in cookie(). These variables are private to their respective functions and are unique; thus the "storage" object you created in orar1() is not being used by the function cookie(). There are a variety of ways to deal with this; for example, you could return the storage object you create in orar1() and then pass it into cookie() or you could create global variable (which I do not recommend). I'm sure there are many other ways to deal with this, depends on how you want to write your JavaScript.

Shahab_L_Intel
Employee
85 Views

Besides what Paul suggested, you can just add this to cookie()

var storage = window.localStorage;

 

Alex_Hang
New Contributor II
85 Views

Thank you very much for your answers.

I found out that I had one more mistake in my code. Even after I did everything you told me, it was still not working. Then I changed the second function's name, from "cookie()", to a random name, "edit()", and now everything works perfect :)

Thank you once again!

Reply