Javascript 使用GoogleAjax库API时,将jQuery注入页面失败
我想使用Google AJAX库API将jQuery注入页面,我提出了以下解决方案: :Javascript 使用GoogleAjax库API时,将jQuery注入页面失败,javascript,jquery,google-ajax-libraries,Javascript,Jquery,Google Ajax Libraries,我想使用Google AJAX库API将jQuery注入页面,我提出了以下解决方案: : ;((function(){ // Call this function once jQuery is available var func = function() { jQuery("body").prepend('<div>jQuery Rocks!</div>'); }; // Detect if page is already using jQu
;((function(){
// Call this function once jQuery is available
var func = function() {
jQuery("body").prepend('<div>jQuery Rocks!</div>');
};
// Detect if page is already using jQuery
if (!window.jQuery) {
var done = false;
var head = document.getElementsByTagName('head')[0];
var script = document.createElement("script");
script.src = "http://www.google.com/jsapi";
script.onload = script.onreadystatechange = function(){
// Once Google AJAX Libraries API is loaded ...
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
// ... load jQuery ...
window.google.load("jquery", "1", {callback:function(){
jQuery.noConflict();
// ... jQuery available, fire function.
func();
}});
// Prevent IE memory leaking
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
}
// Load Google AJAX Libraries API
head.appendChild(script);
// Page already using jQuery, fire function
} else {
func();
}
})());
<html>
<head>
<title>This is my page</title>
</head>
<body>
<h1>This is my page.</h1>
<script src="http://my-domain.com/inject-jquery.js"></script>
</body>
</html>
这个错误似乎特定于GoogleAjax库API,因为我看到其他人使用jQuery书签将jQuery注入当前页面。我的问题:
- 有没有一种方法可以将GoogleAjax库API/jQuery注入到页面中,而不考虑onload/onready状态
(function() {
var script = document.createElement("script");
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
script.onload = script.onreadystatechange = function(){ /* your callback here */ };
document.body.appendChild( script );
})()
在我们想出一个不同的解决方案后,我发现了这篇文章。因此,出于某种原因,如果您不能使用公认的解决方案,此解决方案似乎可以正常工作:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
// jQuery hasn't been loaded... so let's write it into the head immediately.
document.write('<script type="text/javascript" src="/jquery-1.3.2.min.js"><\/script>')
}
</script>
if(typeof jQuery==“未定义”){
//jQuery还没有加载…所以让我们立即将其写入头部。
文件。写入(“”)
}
接受的解决方案的一个问题是,您必须将要在页面上运行的所有代码放入回调函数中。所以任何需要jQuery的东西(比如插件)都需要从该函数调用。而且,所有其他包含的需要jQuery的JS文件都依赖于在所有其他脚本启动之前加载jQuery。我让它工作了!!!!!我通过查看应用程序操场找到了答案 下面是开始使用jquery的包装。。。。在函数中添加警报以查看其工作情况
google.load("jquery", "1.4.2");
function OnLoad(){
$(function(){
});
}
google.setOnLoadCallback(OnLoad);
您可以使用不那么痛苦的解决方案将jquery(可用的最新稳定版本)注入任何页面
-用于将jQuery(可用的最新稳定版本)注入任何网页(甚至HTTPS)的Chrome扩展“更直接的方法,你知道谷歌服务器上是否有一个指向最新jQuery的静态URL吗?用于获取当前版本的最新版本。另一件有趣的事情是,如果你删除回调函数,Google loader工作正常。它似乎只有在它存在的时候才会抱怨。所以,你也可以想出一种不同的方法来检测jquery的存在。我觉得这个解决方案不管用。我的回调似乎从未执行过。
google.load("jquery", "1.4.2");
function OnLoad(){
$(function(){
});
}
google.setOnLoadCallback(OnLoad);