1

I was porting a chrome extension to firefox. Everything is fine in chrome but the following code generates error in firefox.

localStorage.setItem("display_tooltip", false);

data = localStorage.getItem('display_tooltip');
    if(data.display_tooltip)                  //line where error occurs
        display_tooltip = true;
    else
        display_tooltip = false;

I get an error on firefox saying data is null. Where am I doing it wrong. The same code works perfectly when I run the chrome extension. I am using firefox addon-sdk.

Juan John Mathews
  • 728
  • 1
  • 10
  • 24

3 Answers3

3

localStorage stores strings not booleans, hence you'll have to compare accordingly. And you need to check for data variable instead of data.display_tooltip, as:

localStorage.setItem("display_tooltip", false);
data = localStorage.getItem('display_tooltip'); 
// variavle 'data' holds value stored in 'display_tooltip' variable
if(data == "true") {
    //your code
    alert("True");
}
else {
    //false, your code
    alert("False");
}

Demo:: jsFiddle

Community
  • 1
  • 1
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

It will not work in any browser.

You are setting 'false' to the var data so data.display_tooltip does not exists.

Just ask for the value of data:

if (data == 'true')
  display_tooltip = true;
else
 display_tooltip = false;

Note that localStorage does not store booleans so you need to ask for the 'true' string when you check the value.

ianaya89
  • 4,153
  • 3
  • 26
  • 34
0

you data is:

data = localStorage.getItem('display_tooltip')

Why you using data. display_tooltip to check? Justif (data) { ... } . Maybe you need to check the type of data. Maybe it is string of true or false, but not bool.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57