编辑:详细信息已更新,以增加Firefox / Chrome行为的差异
我正在尝试创建一个将同时加载jQuery和jQueryUI的小书签。 jQuery加载使用javascript,但我认为自从jQuery加载以来,我就继续将其用于UI加载。 除了使它工作之外,我真的很想了解为什么这不起作用。 我仍然在围绕示波器/关闭/等。 但是我只是不明白为什么在firefox $中不起作用,而“ jQuery”起作用了! $可以在Chrome中正常运行,但在那里出现了其他问题。
笔记:
1)在FireBug / FireFox中,我得到'$(“ head”)未定义'
2)在Chrome中,“ $”工作正常,但是jQueryUI调用失败,对象[object Object]没有方法'dialog'
3)回调保证在我尝试使用jQuery时已将其加载。 在Firefox中,如果我将“ $”替换为“ jQuery”(例如jQuery(“ head)),那么代码就可以了!
4)页面上没有其他库已在使用$
5)更令人沮丧的是,在Firefox中,如果我只是让步并使用“ jQuery”而不是“ $”,然后从$(“#jquilib”)。load()设置回调以调用第三个函数,例如jQueryUI函数,例如即使回调本身是由jQueryUI触发的,.tabs()和.dialog()也不可用!
6)在Chrome中,如果我将setTimeout()设置为100ms,则jQueryUI问题就消失了。 如果我下降到1毫秒或更长时间,问题仍然存在。
我正在使用以下文章中的getScript函数: http : //www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet 。
下面是我的代码:
function getScript(url,success){
var script=document.createElement('script');
script.src=url;
var head=document.getElementsByTagName('head')[0],
done=false;
// Attach handlers for all browsers
script.onload=script.onreadystatechange = function(){
if ( !done && (!this.readyState
|| this.readyState == 'loaded'
|| this.readyState == 'complete') ) {
done=true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
function initializejQueryUI(){
if (typeof jQuery.ui == 'undefined'){
// jQueryUI library & jQueryUI cupertino theme
$('head').append("<link href='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/cupertino/jquery-ui.css' type='text/css' rel='stylesheet'>");
$('head').append("<script id='jquilib' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>");
}
$("#jquilib").load($("<div>jQuery & UI Loaded!</div>").dialog());
}
getScript('https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js', initializejQueryUI); // call initializejQueryUI as callback when jQuery loads