如何使用jQuery同步加载页面

Kar*_*ran 2 html javascript jquery load

目前我正在使用jQuery来选择我的索引页面应该加载什么,这取决于是否设置了cookie.

例如,如果设置了cookie,则index.html的body标签应加载hasCookie.html,如果未设置cookie,则加载noCookieSet.html.

我的索引页面目前有:

load user scripts
load styling scripts

<html><body></body></html>
Run Code Online (Sandbox Code Playgroud)

用户脚本具有检查cookie是否已设置的代码.如果是,我使用jQuery的加载将内容加载到正文中:

$(document.body).load('hasCookie.html')
Run Code Online (Sandbox Code Playgroud)

但是,由于这是异步执行的,因此在更新body标记之前已加载样式脚本,因此未对样式进行样式设置.

有没有办法同步加载,或者我应该以错误的方式接近这个?

编辑:如果它有帮助,样式脚本基本上是jQueryUI

Pet*_*son 9

AFAIK,JQuery加载不能同步.

你可以用回调函数来解决这个问题.

$(document.body).load('hasCookie.html', function(){
//call styling script here
});
Run Code Online (Sandbox Code Playgroud)