在chrome扩展中的content_script中没有加载jquery文件

我是这个chrome扩展插件开发的新手。 我正在使用这个chrome扩展开发一个新的新插件。

我的要求是在没有js文件时将jquery.js文件加载到内容脚本中(例如:我正在检查jquery.js文件,fancybox.js文件,如果没有加载这些文件。)

当我在内容脚本中实现此逻辑时,它正在加载jquery.js文件。 之后,它无法在内容脚本中工作。 它显示$(或)jQuery未定义。 每次加载时,但不在内容脚本中执行。

这是我实现的代码。

document.getElementById('someid').onclick = loadJs(); function loadJS() { if(tyof jQuery == undefined || tyof jQuery != 'function' ) { var jqscript = document.createElement('script'); jqscript.type = 'text/javascript'; jqscript.async = true; // Src of the script jqscript.src = "......url to jquery.js file.............."; // Check whether script is loaded or not jqscript.onload=function(){ if ((!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) { console.log("Script loaded - "); // It is showing error here alert(typeof jQuery); } }; // Get the document header var head = document.getElementsByTagName('head')[0]; // Add script to header - otherwise IE complains head.appendChild(jqscript); } } 

请对此提出建议。

谢谢。

Chrome扩展程序无法访问网页加载和使用的脚本。

谷歌称这个孤立的世界:

http://code.google.com/chrome/extensions/content_scripts.html

你不能…

使用其扩展页面定义的变量或函数

使用由网页或其他内容脚本定义的变量或函数

所以,那里有什么脚本并不重要。 重要的是您在清单中包含所需的脚本:

 "content_scripts": ['jquery.js', 'myScript.js', 'optionsScript.js'], 

列表的顺序很重要,如果你的脚本使用jquery,那么jquery.js应该在它之前。