简体   繁体   English

在js文件中包含jQuery

[英]include jquery in js file

I am developing chrome extension, it is a simple extension which call background.js file to do simple job. 我正在开发chrome扩展程序,它是一个简单的扩展程序,它调用background.js文件来完成简单的工作。 In background.js i want to include jquery.js file to call ajax function. 在background.js中,我想包含jquery.js文件来调用ajax函数。 But i can't find anyway to include jquery.js in background.js. 但是我仍然找不到在background.js中包含jquery.js的内容。 is it possiable to do that? 可以这样做吗?

you know, extension call background.js and it does not call php or html file which include background.js so i can't use anything get involved in php or html 你知道,扩展名为background.js,它不调用包含background.js的php或html文件,所以我不能使用任何涉及php或html的东西

Thanks in advance.... 提前致谢....

It sounds like you're using manifest_version 2, which means you can just add path-to-jquery.js to the background.scripts array. 听起来您正在使用manifest_version 2,这意味着您可以将path-to-jquery.js添加到background.scripts数组中。

"background": {
    "scripts": ["jquery.js", "background.js"]
}

Those files all get loaded in an generated page together, so they access the same global scope—which means you can use jQuery as usual. 这些文件全部一起加载到生成的页面中,因此它们访问相同的全局范围-这意味着您可以照常使用jQuery

Here ya go: 你去:

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);

You will have to put everything in the onload event handler, otherwise, the rest of the JavaScript will continue running even if jQuery hasn't loaded yet. 您将必须将所有内容放入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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM