我使用的代码是from a blog,这与this SO question的答案中的代码非常相似。只有在不存在的情况下才加载jQuery时遇到问题
我的问题是,代码将jQuery包括在内,只包括,但由于某些原因,脚本之后的脚本没有意识到已加载jQuery。我究竟做错了什么?
I've placed the code in a jsFiddle.
if (!window.loadjQueryOnce)
{
function loadjQueryOnce(jQueryVersion)
{
jQueryVersion = typeof(jQueryVersion) != 'undefined' ? jQueryVersion : "1.7.2";
if (typeof jQuery === "undefined") {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jquery/" + jQueryVersion + "/jquery.min.js")
document.getElementsByTagName("head")[0].appendChild(script_tag);
}
}
}
你在做什么应用程序,你需要在加载之前检查jQuery? – saluce
这是我的网页上的日常使用。一些页面中包含的一些自定义脚本需要jQuery,并且让他们在代码中包含这些脚本,所以如果我或其他程序员忘记包含jQuery,那么所包含的脚本仍然可以运行。看起来更容易记住包含它。 –