如何使用jQuery同步加载页面 [英] How to load page synchronously using jQuery

查看:141
本文介绍了如何使用jQuery同步加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用jQuery来选择我的索引页面应该加载的内容,具体取决于是否设置了cookie。



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



我的索引页目前有:

 加载用户脚本
加载样式脚本

< html>< body>< / body>< / html>

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

$ p $ $ document $ body $(document.body).load(' hasCookie.html')

然而,由于这是异步执行的,所以样式脚本在body标签已被更新,因此身体没有风格。



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

p>

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

解决方案

就你而言,这听起来像是如果可能的话实现服务器端真的是一个更好的主意。在PHP中,检测您所需的cookie是否设置在$ _COOKIE数组中并输出正确的页面会很简单。


Currently I am using jQuery to choose what my index page should load depending whether a cookie is set.

For example, the body tag of the index.html should load hasCookie.html if the cookie is set and noCookieSet.html if no cookie is set.

My index page currently has:

load user scripts
load styling scripts

<html><body></body></html>

The user script has the code that checks whether the cookie is set. If it is, I use jQuery's load to load the content into the body:

$(document.body).load('hasCookie.html')

However, since this is executed asynchronously, the styling scripts have loaded before the body tag has been updated, hence the body is not styled.

Is there a way of having a synchronous load, or should am I approaching this in the wrong way?

EDIT: if it helps, the styling scripts are basically jQueryUI

解决方案

In your case, this sounds like it would really be a better idea to implement server-side if possible. In PHP it would be a simple matter of detecting if the cookie you want is set in the $_COOKIE array and outputting the correct page.

这篇关于如何使用jQuery同步加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆