如何提交一个Ajax请求之前的页面加载 [英] How do i submit an ajax request before the page is loaded
问题描述
我想通过一个Ajax请求检查用户的登录状态。根据阉的用户登录我要显示任何的用户名/密码输入或用户名。目前的请求被发送上body.onload和一个prgoress指示器示出,直到响应到达。有没有更好的办法?
I want to check the login status of a user through an ajax request. Depending wether the user is logged in I want to display either the username/password input or the username. Currently the request is sent on body.onload and a prgoress indicator is shown until the response arrives. Is there a better way?
让我们假设该要求规定,应该没有直接的服务器端处理。
Let's assume that the requirements state that there should be no direct server side processing.
推荐答案
如果你不希望依赖于一个工具包,您可以创建自己的domready中函数,看起来有点像这样的:
If you don't want to depend on a toolkit, you can create your own DOMReady function that looks kinda like this:
/* Usage: DOMReady(ajaxFunc); */
function DOMReady(f) {
if (!document.all) {
document.addEventListener("DOMContentLoaded", f, false);
} else {
if (document.readystate == 'complete') {
window.setTimeout(f, 0);
}
else {
//Add event to onload just if all else fails
attachEvent(window, "load", f);
}
}
}
或者一个更复杂的解决方案: http://snipplr.com/view/6029/domreadyjs/
这篇关于如何提交一个Ajax请求之前的页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!