Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
Posts: 43
Registered: ‎09-09-2011
My Device: 9900
My Carrier: airtel

store javascript object into cookie

How do we store javascript object into cookie. I tried below but it did not work for me

 

var call_login = function() {

alert("Login Clicked")

}

 

bb_login = new blackberry.ui.menu.MenuItem(false, 0, "Login", call_login);

 

document.cookie = "cookie_login="+bb_login;

Contributor
Posts: 10
Registered: ‎01-22-2012
My Device: Playbook 16GB
My Carrier: None

Re: store javascript object into cookie

Why do you want to store the menu item in a cookie? There might be a better way to handle what you're trying to do.

 

However, here's a way to do it with simple objects.

 

http://www.terminally-incoherent.com/blog/2008/11/25/serializing-javascript-objects-into-cookies/

 

Mark

 

Contributor
Posts: 43
Registered: ‎09-09-2011
My Device: 9900
My Carrier: airtel

Re: store javascript object into cookie

Actually I am adding the menu to menu button when My webworks app loading. so cause the memory leak issue for me when i open thi for the sixth time exactly. So i want to restrict the object initiation to only once. Every time when I load my app, I don't want to initiatte the object again. So I wanted this to put into the cookie and use it. But I check the above link also did not work for me. Is there any other way to the things.

Contributor
Posts: 10
Registered: ‎01-22-2012
My Device: Playbook 16GB
My Carrier: None

Re: store javascript object into cookie

If the app is for the Playbook you can try using localStorage

 

https://bdsc.webapps.blackberry.com/html5/apis/localStorage.html

 

Mark

 

Contributor
Posts: 43
Registered: ‎09-09-2011
My Device: 9900
My Carrier: airtel

Re: store javascript object into cookie

There is no support to use localStorage in blackberry smartphone 6.0  version. I need something like localstorage or cookie functionality. 

Developer
Posts: 214
Registered: ‎01-07-2011
My Device: Bold 9900
My Carrier: Rogers

Re: store javascript object into cookie

Have you tried to use localstorage on BBOS 6?

I believe there is an omission in the API documentation that has yet to be corrected. I am pretty sure it is supported even though the API says Playbook only.

--
Rory (@roryboy)
Click "Accept as Solution" if post solved your original issue. Give like/thumbs up if you feel post is helpful
Contributor
Posts: 43
Registered: ‎09-09-2011
My Device: 9900
My Carrier: airtel

Re: store javascript object into cookie

Yes I tried. But I am unable to store into the localstorage variable.
Developer
Posts: 214
Registered: ‎01-07-2011
My Device: Bold 9900
My Carrier: Rogers

Re: store javascript object into cookie

What is the code snippet you tried to do your reading and writing from local storage?

--
Rory (@roryboy)
Click "Accept as Solution" if post solved your original issue. Give like/thumbs up if you feel post is helpful
Contributor
Posts: 43
Registered: ‎09-09-2011
My Device: 9900
My Carrier: airtel

Re: store javascript object into cookie

Here is the code i am trying to do with localstorage

 

var var_login_storage=localStorage.getItem("login_storage");		
if(!var_login_storage)
{		
        bb_login = new blackberry.ui.menu.MenuItem(false, 0, "Login", call_login);			
	localStorage.setItem("login_storage", bb_login);	
}	

 

Developer
Posts: 214
Registered: ‎01-07-2011
My Device: Bold 9900
My Carrier: Rogers

Re: store javascript object into cookie

You need to turn the object into a string before you can store it as localStorage can only be used for strings. This means when you get it out agian you need to turn it back into an object, which is why JSON.stringify and JSON.parse are being used.

 

When checking to see if something exists in localStorage I have found checking against null works consistently.

 

So try this:

var var_login_storage=localStorage.getItem("login_storage");
if(var_login_storage != null){
var_login_storage = JSON.parse(var_login_storage)
}else{ bb_login = new blackberry.ui.menu.MenuItem(false, 0, "Login", call_login); localStorage.setItem("login_storage", JSON.stringify(bb_login)); }

 

R

--
Rory (@roryboy)
Click "Accept as Solution" if post solved your original issue. Give like/thumbs up if you feel post is helpful