在这篇文章之后,我设法推迟了 JS 库的加载以及一个文档就绪功能。可能推迟加载 jQuery?
但是,我有多个文档就绪功能,它们由不同的模块放置在页面中(而不是在每个页面上)。我到目前为止的代码:
echo'
// deferred loading of jQuery library and
(function() {
function getScript(url,success){
var script=document.createElement("script");
script.src=url;
var head=document.getElementsByTagName("head")[0],
done=false;
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);
}
getScript("lib/jquery/js/jquery-1.4.4.min.js",function(){
getScript("lib/jquery/js/jquery.tools.min.js",function(){
$("ul.tabs").tabs("div.panes > .pane", {
';
if(!isset($params['onclickfunction']) == 'no') {
echo '
onClick: function() {
var myTab = this.getCurrentTab().text();
document.getElementById("titleReplacement").innerHTML = " - " + myTab;
},
'; } //end conditional click function
echo '
effect:"fade",
fadeInSpeed:800,
initialIndex:';
if(isset($_GET['tabs'])) { $this_url = $_GET['tabs']; }
else { $this_url = 'some text'; }
if($this_url == $params['tab1']) { echo '0'; }
elseif ($this_url == $params['tab2']) { echo '1'; }
elseif ($this_url == $params['tab3']) { echo '2'; }
//nothing matches? show first tab
else { echo '0'; }
echo ' }) ;
});
}) // possibly another ; here ???
})();
';
我认为这实际上可行,但其他文档就绪功能正在尝试在加载库之前运行。是否有一个简单的测试可以用于其他功能,因为我无法将它们合并到一个功能中。
干杯