Skip to content

Instantly share code, notes, and snippets.

@kukat
Created March 24, 2012 19:02
Show Gist options
  • Save kukat/2186755 to your computer and use it in GitHub Desktop.
Save kukat/2186755 to your computer and use it in GitHub Desktop.
Javascript: Load jQuery
load = function() {
load.getScript("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
load.tryReady(0); //用来等待jQuery类库加载
}
//以下代码用来动态加载任何js代码
load.getScript = function(filename) {
var script = document.createElement('script')
script.setAttribute("type","text/javascript")
script.setAttribute("src", filename)
if (typeof script!="undefined")
document.getElementsByTagName("head")[0].appendChild(script)
}
load.tryReady = function(time_elapsed) {
//持续查询jQuery是否加载完毕
if (typeof jQuery == "undefined") { // 如果没有加载jQuery...
if (time_elapsed <= 5000) {
setTimeout("load.tryReady(" + (time_elapsed + 200) + ")", 200); //设置200ms后继续尝试
} else {
alert("Failed to loading jQuery because of timeout.")
}
} else {
// jQuery加载完毕后执行的代码
alert("Great! jQuery is loaded.");
}
}
load();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment