Avatar of CrAppl
CrAppl

asked on 

Firefox addon: Checking, setting and getting from localstorage

Hi!

In the Google Chrome browswer this works perfectly:
if(localStorage['list_expiry_date'] === undefined) 
{alert("in local storage");
localStorage['list_expiry_date'] ="P";}
else
		{alert("Local storage exists and has the value of: "+localStorage['list_expiry_date']);}

Open in new window


But in firefox 4 it's throwing an error, so my question is:
How do you
1- check to see if localStorage['list_expiry_date'] already exists?
2- set the value for localStorage['list_expiry_date']
3-get the value from localStorage['list_expiry_date']

Thanks!
JavaScriptWeb BrowsersWeb Development

Avatar of undefined
Last Comment
CrAppl
Avatar of GregArnott
GregArnott
Flag of Australia image

I believe Firefox uses the syntax localStorage.getItem() for its Storage
Thus:
//  1- check to see if localStorage['list_expiry_date'] already exists?
if(   localStorage.getItem('list_expiry_date') ) {

}

// 2- set the value for localStorage['list_expiry_date']
 localStorage.setItem('list_expiry_date', value)

// 3-get the value from localStorage['list_expiry_date']
 localStorage.getItem('list_expiry_date')

Open in new window

Avatar of GregArnott
GregArnott
Flag of Australia image

Found this link which may help:
Firefox and Storage
Avatar of CrAppl
CrAppl

ASKER

Thanks for replying!

This is the error I gett when running that code:
Error: uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMStorageWindow.localStorage]"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: chrome://quickfilter/content/quickfilter.js :: <TOP_LEVEL> :: line 93"  data: no]

The exact code is:
if(   localStorage.getItem('list_expiry_date') )
{
	alert("in local storage");
	localStorage.setItem('list_expiry_date',"P")
}
else
		{alert("Local storage exists and has the value of: "+localStorage.getItem('list_expiry_date'));}

Open in new window


Do I have to have some code before that that specifies that I am using localstorage or soemthing?
ASKER CERTIFIED SOLUTION
Avatar of FireAnICE
FireAnICE

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CrAppl
CrAppl

ASKER

That worked! thanks!
Avatar of CrAppl
CrAppl

ASKER

That worked, thanks, can you also check my other questions please?
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo