0

I am passing a message from my content script to my background.js.

I want the message to alter the global variable foo in the below code. The message passes without issue and the first alert() works properly, but the second alert() uses the original foo definition.

How can I make the updated foo the global variable, such that the second alert() results in second, not first?

background.js

var foo = "first"

// Other code here...

chrome.runtime.onMessage.addListener(function(msg, _) {
    foo = msg.passedMessage; // where passedMessage = "second"
    alert(foo) // "second"
});

alert(foo) // "first"
JSW189
  • 6,267
  • 11
  • 44
  • 72

0 Answers0