Reputation: 39
I am developing chrome extension, it is a simple extension which call background.js file to do simple job. In background.js i want to include jquery.js file to call ajax function. But i can't find anyway to include jquery.js in background.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
Thanks in advance....
Upvotes: 0
Views: 3412
Reputation: 12672
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.
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);
Upvotes: 0
Reputation: 321
It sounds like you're using manifest_version 2, which means you can just add path-to-jquery.js
to the background.scripts array.
"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.
Upvotes: 4