dortonway
dortonway

Reputation: 439

Chrome extension: Access to variables of a background.js from a popup.js

If I'm using chrome.extension.getBackgroundPage(), I can access variables of the background.js like this:

background.js:

var transfer = 'some text';

popup.js:

chrome.extension.getBackgroundPage().transfer

But this says I get only a window object (but maybe 'JavaScript' before 'window' means something...). How can I access background variables?

Upvotes: 10

Views: 12839

Answers (1)

Ryan
Ryan

Reputation: 5682

Yes, the word 'javascript' before window does mean that its returning the javascript file(page) background.js

For ease of access at the top of my popup.js file I do this:

var background = chrome.extension.getBackgroundPage(); //do this in global scope for popup.js

then you can do this:

background.transfer;

Since you typically have to access your background page a lot, this just makes life easier all around.

Upvotes: 18

Related Questions