我正在制作一对小部件,这些小部件可以单独包含或作为一对包含在内,并且没有特别的顺序。我确保jQuery是加载通过使用这个脚本:检查是否包含jQuery
if (typeof jQuery == 'undefined') {
//alert("about to load jquery");
var esm_script_tag = document.createElement('script');
esm_script_tag.setAttribute("type","text/javascript");
esm_script_tag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js")
esm_script_tag.onload = main; // Run main() once jQuery has loaded
esm_script_tag.onreadystatechange = function() { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') main();
}
document.getElementsByTagName("head")[0].appendChild(es_script_tag);
}
else{
main();
}
在一个脚本,它触发功能main()
和其他main2()
。问题是,一旦第一个脚本运行,它开始包括jQuery。当第二个运行时,jQuery尚未加载,但不会再加载它以启动功能main2()
。
有没有更好的方法来包含jQuery?有没有办法让我的小部件检查jQuery是否处于被加载的状态并保持到它准备好?
感谢您的回复。
如果你在文档准备好了这个测试呢? – DOK 2012-01-10 21:11:11
@DOK这不是多余的吗? – Blazemonger 2012-01-10 21:14:25
但是$(document).ready在jQuery加载之前不可用。 – emachine 2012-01-10 21:14:29