在执行之前等待脚本加载
Wait for script to load before executing
我想让下面的代码在执行之前等待另一个脚本加载。我怎样才能让它在执行前等待 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');
});
延迟执行代码最简单的方法是使用回调。例如,如果您有一个名为 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);
希望对您有所帮助!
来自评论的回答。请检查 jQuery documentation.
你只需要使用回调。如果你想在 .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();
});
};
我想让下面的代码在执行之前等待另一个脚本加载。我怎样才能让它在执行前等待 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');
});
延迟执行代码最简单的方法是使用回调。例如,如果您有一个名为 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);
希望对您有所帮助!
来自评论的回答。请检查 jQuery documentation.
你只需要使用回调。如果你想在 .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();
});
};