如何在加载页面之前提交ajax请求

时间:2022-06-15 05:46:56

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?

我想通过ajax请求检查用户的登录状态。根据用户的登录情况,我想显示用户名/密码输入或用户名。目前,请求在body.onload上发送,并显示一个prgoress指示符,直到响应到达。有没有更好的办法?


Let's assume that the requirements state that there should be no direct server side processing.

我们假设需求声明不应该直接进行服务器端处理。

5 个解决方案

#1


2  

If you don't want to depend on a toolkit, you can create your own DOMReady function that looks kinda like this:

如果您不想依赖工具包,可以创建自己的DOMReady函数,看起来有点像这样:

/* 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);
        }
    }
}

Or for a more complex solution: http://snipplr.com/view/6029/domreadyjs/

或者更复杂的解决方案:http://snipplr.com/view/6029/domreadyjs/

#2


6  

This sounds like an operation that should be done on the server first, before the page is rendered. If someone has javascript disabled, what would happen?

这听起来像是应该在呈现页面之前首先在服务器上完成的操作。如果有人禁用了javascript,会发生什么?

#3


1  

Why not check before the user is even given the html?

为什么不在用户甚至给出html之前检查?

If you're just running static HTML w/ Javascript, I would suggest using JQuery and using the $(document).ready():

如果您只是运行带有Javascript的静态HTML,我建议使用JQuery并使用$(document).ready():

#4


0  

I think this should be possible, but it is not recommended. The correct thing to do is have the server fully determine the content of the page before it is even sent.

我认为这应该是可能的,但不建议这样做。正确的做法是让服务器在发送之前完全确定页面的内容。

If you're being held up by slow image downloads or other non-HTML content, check out one of the various JavaScript libraries. I always recommend jQuery, which has the following syntax:

如果您被慢速图像下载或其他非HTML内容所困扰,请查看各种JavaScript库之一。我总是推荐jQuery,它具有以下语法:

$(document).ready(function() {
  // The DOM is fully loaded now, but images might still be loading.
});

#5


0  

I believe that if you use inline javascript, it should be executed as soon as it's encountered:

我相信如果你使用内联javascript,它应该在遇到它时立即执行:

<HTML><HEAD>...</HEAD>
<BODY>
<script>
document.write("This is written before anything else");
</script>
This come later.
</BODY>
</HTML>

#1


2  

If you don't want to depend on a toolkit, you can create your own DOMReady function that looks kinda like this:

如果您不想依赖工具包,可以创建自己的DOMReady函数,看起来有点像这样:

/* 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);
        }
    }
}

Or for a more complex solution: http://snipplr.com/view/6029/domreadyjs/

或者更复杂的解决方案:http://snipplr.com/view/6029/domreadyjs/

#2


6  

This sounds like an operation that should be done on the server first, before the page is rendered. If someone has javascript disabled, what would happen?

这听起来像是应该在呈现页面之前首先在服务器上完成的操作。如果有人禁用了javascript,会发生什么?

#3


1  

Why not check before the user is even given the html?

为什么不在用户甚至给出html之前检查?

If you're just running static HTML w/ Javascript, I would suggest using JQuery and using the $(document).ready():

如果您只是运行带有Javascript的静态HTML,我建议使用JQuery并使用$(document).ready():

#4


0  

I think this should be possible, but it is not recommended. The correct thing to do is have the server fully determine the content of the page before it is even sent.

我认为这应该是可能的,但不建议这样做。正确的做法是让服务器在发送之前完全确定页面的内容。

If you're being held up by slow image downloads or other non-HTML content, check out one of the various JavaScript libraries. I always recommend jQuery, which has the following syntax:

如果您被慢速图像下载或其他非HTML内容所困扰,请查看各种JavaScript库之一。我总是推荐jQuery,它具有以下语法:

$(document).ready(function() {
  // The DOM is fully loaded now, but images might still be loading.
});

#5


0  

I believe that if you use inline javascript, it should be executed as soon as it's encountered:

我相信如果你使用内联javascript,它应该在遇到它时立即执行:

<HTML><HEAD>...</HEAD>
<BODY>
<script>
document.write("This is written before anything else");
</script>
This come later.
</BODY>
</HTML>