Javascript 等待脚本加载后再执行
我想让下面的代码在执行之前等待加载另一个脚本。如何让它在执行之前等待sp.js加载Javascript 等待脚本加载后再执行,javascript,jquery,sharepoint,Javascript,Jquery,Sharepoint,我想让下面的代码在执行之前等待加载另一个脚本。如何让它在执行之前等待sp.js加载 $.getScript("/sites/MySite/SiteAssets/jquery.LISP.Quicklaunch.js", function(){}); jQuery(document).ready(function() { jQuery('#s4-leftpanel').quicklaunchHide({ collapseImage: '/_layouts/images/mewa_leftP
$.getScript("/sites/MySite/SiteAssets/jquery.LISP.Quicklaunch.js", function(){});
jQuery(document).ready(function() {
jQuery('#s4-leftpanel').quicklaunchHide({
collapseImage: '/_layouts/images/mewa_leftPage.gif',
expandImage: '/_layouts/images/mewa_rightPage.gif',
prependLocation: '.s4-tn',
mainDiv: '.s4-ca',
leftMargin: 0,
hideOnDefault: true,
allowCookie: true,
cookieName: 'quicklaunchStatus'
});
function setHeight() {
windowHeight = $(window).innerHeight();
$('#s4-mainarea').css('min-height', windowHeight-203);
};
setHeight();
$(window).resize(function() {
setHeight();
});
jQuery('#s4-workspace').css('cssText', 'height: 262px !important');
});
延迟代码的执行最简单的方法是使用回调。例如,如果您有一个名为
defer()
的函数,您只想在函数dofirst()
执行后执行该函数,您应该执行以下操作:
function dofirst(arguments, callback){
//Do something here first, then call the callback
callback();
};
function defer(arguments){
//This is the code which you want to defer until the dofirst code has executed
return;
}
//Invoke the function here
dofirst(arguments, defer);
希望这有帮助 根据评论回答。请检查 您只需要使用回调。如果要在
.getScript()
之后执行代码,请将其放入第二个参数中,该参数当前是一个回调函数:
$.getScript('path/to/script.js', function(){
/* your code goes here */
});
我最终是这样做的:
$.getScript("/sites/MySite/SiteAssets/jquery.LISP.Quicklaunch.js", function(){});
jQuery(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(delayedExecute, "sp.js");
});
function delayedExecute(){
jQuery('#s4-leftpanel').quicklaunchHide({
collapseImage: '/_layouts/images/mewa_leftPage.gif',
expandImage: '/_layouts/images/mewa_rightPage.gif',
prependLocation: '.s4-tn',
mainDiv: '.s4-ca',
leftMargin: 0,
hideOnDefault: true,
allowCookie: true,
cookieName: 'quicklaunchStatus'
});
function setHeight() {
windowHeight = $(window).innerHeight();
listHeight = windowHeight - 180;
jQuery('#s4-workspace').css('cssText', 'height: ' + listHeight + 'px !important');
};
setHeight();
$(window).resize(function() {
setHeight();
});
};
jQuery文档中有一个例子——“加载新功能后绑定一些彩色动画”:什么
sp.js
?您只需要使用回调。如果要在.getScript()
之后执行代码,请将其放入第二个参数中,该参数是一个函数:$.getScript('path/to/script.js',function(){/*此处的代码*/})
Great answer@kmsdev,这是实现这一点的正确方法。请给出一个完整的答案,以获得信任,并结束这个问题。我把它作为评论,因为我不完全确定这是否是OP想要做的。这就是它的外观,但我不确定.OP是否要求一个名为sp.js
的文件,该文件没有出现在发布的代码中。而且他的回答与他的问题完全不符。。。问题还不清楚。疑问已经消除@fond42518