在我的chrome扩展程序的background.html中,我有:
$(document).ready(function(){
var some_variable = "some_variable";
function some_function() { alert("some_function"); }
});
在popup.html中我有:
$(document).ready(function(){
var bg = chrome.extension.getBackgroundPage();
alert(bg.some_variable);
bg.somefunction();
});
当我运行程序时......
bg.some_variable出现为未定义,
运行该函数时会出现以下错误:
Object [object DOMWindow] has no method 'some_function'
我在想因为some_variable和some_function都是在background.html的$(document).ready事件的回调中定义的,所以它正在搞乱popup.html对这些声明的访问。
有解决方法吗?我错过了什么?
提前感谢您的时间。
答案 0 :(得分:2)
将您的变量放入全局范围:
var some_variable;
$(document).ready(function(){
some_variable = "some_variable";
}