我想获取下面的代码,等待在执行之前加载另一个脚本。如何在执行之前等待sp.js加载?
$.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');
});
答案 0 :(得分:2)
评论回答。请查看 jQuery documentation 。
您只需要使用回调即可。如果要在.getScript()
之后执行代码,请将其放在第二个参数中,该参数当前是回调函数:
$.getScript('path/to/script.js', function(){
/* your code goes here */
});
答案 1 :(得分:0)
推迟执行代码最简单的方法是使用回调。例如,如果你有一个名为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);
希望这有帮助!
答案 2 :(得分:0)
我最终这样做了:
$.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();
});
};