Mitya
Mitya

Reputation: 1023

jquery in background.html won't let me access background variables and functions in popup.html

In background.html of my chrome extension I have:

$(document).ready(function(){
    var some_variable = "some_variable";

    function some_function() { alert("some_function"); }
});

In popup.html I have:

$(document).ready(function(){

    var bg = chrome.extension.getBackgroundPage();

    alert(bg.some_variable);

    bg.somefunction();
});

When I run the program...

bg.some_variable comes up as undefined,

when the function is run the following error comes up:

Object [object DOMWindow] has no method 'some_function'

I am thinking that because both some_variable and some_function are defined inside a callback for the $(document).ready event in background.html, it is messing with popup.html's access to those declarations.

Is there a way around this? What am I missing?

Thanks in advance for your time.

Upvotes: 0

Views: 468

Answers (1)

serg
serg

Reputation: 111365

Put your variable into a global scope:

var some_variable;
$(document).ready(function(){
    some_variable = "some_variable";
}

Upvotes: 2

Related Questions