Firefox addon: Checking, setting and getting from localstorage

CrAppl
CrAppl used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Found this link which may help:
Firefox and Storage

Author

Commented:
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?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Try this:

var url = "http://anydomainnamehere.com";
var ios = Components.classes["@mozilla.org/network/io-service;1"]
          .getService(Components.interfaces.nsIIOService);
var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
          .getService(Components.interfaces.nsIScriptSecurityManager);
var dsm = Components.classes["@mozilla.org/dom/storagemanager;1"]
          .getService(Components.interfaces.nsIDOMStorageManager);

var uri = ios.newURI(url, "", null);
var principal = ssm.getCodebasePrincipal(uri);
var storage = dsm.getLocalStorageForPrincipal(principal, "");

//storage.setItem("chromekey", "chromevalue");




        try
        {
           
if(   storage.getItem('list_expiry_date')==null )
{
	alert("in local storage");
	storage.setItem('list_expiry_date',"P")
}
else
		{alert("Local storage exists and has the value of: "+storage.getItem('list_expiry_date'));}
}
        catch(e){
        alert("ERROR2: \n"+e.toString());
        }

Open in new window

Author

Commented:
That worked! thanks!

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial