开发者

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.


Put your variable into a global scope:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜