我在Joomla工作,想要添加一个jQuery脚本。我的index.php看起来像这样:
<script>
if (typeof jQuery == 'undefined')
{
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">var $j = jQuery.noConflict();</script>
<script type="text/javascript" src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/myjqueryscript.js"></script>
我想要做的是在没有其他jQuery加载的情况下加载jQuery,如果已经加载了jQuery,则不加载它。
答案 0 :(得分:2)
您应该等到页面上加载jQuery后再使用它。试试这个。
function ScriptLoaded(){
alert('jQuery Loaded');
}
if (typeof jQuery == 'undefined') {
var script= document.createElement('script');
script.type= 'text/javascript';
script.onreadystatechange= function () {
if (this.readyState == 'complete'){
ScriptLoaded();
}
}
script.onload= ScriptLoaded;
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
}
<强> DEMO 强>