我正在开发铬扩展,它是一个简单的扩展,调用background.js文件来做简单的工作。在background.js中,我想包含jquery.js文件来调用ajax函数。但是我找不到在background.js中包含jquery.js的方法。这样做有可能吗?
你知道,扩展调用background.js,它不调用包含background.js的php或html文件,所以我不能使用任何与php或html相关的东西。
提前感谢....
发布于 2012-07-15 02:20:21
这就对了:
var jQueryElement=document.createElement('script');
jQueryElement.type = 'text/javascript';
jQueryElement.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(jQueryElement);您必须将所有内容都放在onload事件处理程序中,否则,即使jQuery尚未加载,JavaScript的其余部分也将继续运行。
var jQueryElement=document.createElement('script');
jQueryElement.type = 'text/javascript';
jQueryElement.onload = function () {
alert('jQuery loaded');
};
jQueryElement.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(jQueryElement);发布于 2012-07-15 02:14:18
听起来您使用的是manifest_version 2,这意味着您只需将path-to-jquery.js添加到background.scripts数组中。
"background": {
"scripts": ["jquery.js", "background.js"]
}所有这些文件都加载到一个生成的页面中,因此它们访问相同的全局作用域-这意味着您可以照常使用jQuery。
https://stackoverflow.com/questions/11486141
复制相似问题