Braiam Braiam - 6 days ago 5
Javascript Question

`var status = true` is a string in Chrome Extension, why?

I'm having a piece of code very simple. It checks two parameters. If first parameter is false it change the popup icon in the chrome extension, if it's true and previously was false, then it change the icon backs and if no parameters is passed it returns the current values.

The problem is that either way, both the variable and the first parameter that is being passed as boolean, is interpreted as string and that produce unexpected results. Here's my code:

var status = true;
var reason;
function isReady(statusT, reasonT) {
"use strict";
if (statusT === false) {
chrome.browserAction.setIcon({
path : "img/blue-sharingan.png"
});
status = statusT;
if (reason !== undefined) {
reason = reasonT;
return;
}
return;
}
if (statusT === true && status === false) {
chrome.browserAction.setIcon({
path : "img/icon-32.png"
});
status = statusT;
reason = null;
return;
}
if (statusT === undefined && reasonT === undefined) {
return {
'status' : status,
'reason' : reason
};
}
}


I've read http://stackoverflow.com/a/653934/792066 and I still don't figure out what could be wrong.

Answer

The problem here is what status is attached to. You are using it off the global/window scope.

Back in the good ole days we were able to set the text in the status bar. How you would do it is by setting window.status to a string value. So what you are doing is NOT setting a variable, but changing the string of the browser's status bar.