jQuery的ajaxError()处理程序便会启动,如果用户离开网页页面加载完毕前 [英] jQuery ajaxError() handler fires if user leaves page before page finishes loading

查看:157
本文介绍了jQuery的ajaxError()处理程序便会启动,如果用户离开网页页面加载完毕前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用jQuery的全球ajaxError()处理程序,以提醒任何AJAX失败的用户:

  $(文件).ajaxError(函数(){
    $(有一个网络或服务器错误,请稍后再试。)。对话框({
        标题:错误,
        模式:真正的,
        可调整大小:假的,
        按钮:{确定:函数(){(本).dialog(亲密); }}
    });
});
 

不幸的是,这一全球性的错误处理程序,如果用户离开网页就完成加载之前,也激发。下面是步骤重现错误:

  1. 在用户访问网页A,其中包括通过AJAX加载内容。
  2. 在第一个AJAX元素开始加载。
  3. 在用户点击链接访问网页B的 的第A AJAX元素已完成加载之前。
  4. 在错误对话框中简要之前,浏览器重定向到网页B出现。

任何想法,我们如何才能让ajaxError()不触发时,该错误造成的直接访问新页面的用户?

更新:这是我的code现在,纳入建议中的意见之后:

  //我加了3秒钟的延迟,以我们的错误对话框,有足够的时间
//为用户离开一个新页:
$(文件).ajaxError(函数(){
    的setTimeout(my_error_handler,3000);
});

//前离开页面,如果AJAX仍在加载警告用户。
//不知道我会继续这样的:
$(文件).ajaxStart(函数(){
    ajaxing = TRUE;
});

$(文件).ajaxStop(函数(){
    ajaxing = FALSE;
});

window.onbeforeunload =功能(){
    如果(typeof运算(ajaxing)='不确定'和;!&安培; ajaxing){
        回归页面元素仍然加载!;
    }
};
 

解决方案

嗯,是的,如果用户导航到另一个页面,该XHR将中止,这实际上是,一个不成功的通信。一个解决方案是注册一个侦听 onbeforeunload的事件,并解除绑定 ajaxError 在那里,基本上都说:用户即将离开页面,所以我不希望被告知可能跟随阿贾克斯的错误。

一些网站,但是,如Gmail,请周围的其他方法:如果您尝试从页面导航离开,同时还有一些悬而未决的Ajax请求,它给你一个提示,说你有一些进程仍在运行,并让用户决定是否留在现场,直到请求完成,或继续前进,导航离开。

We use jQuery's global ajaxError() handler to alert the user of any AJAX failures:

$(document).ajaxError(function() {  
    $("There was a network or server error. Please try again later.").dialog({  
        title: "Error",  
        modal: true,  
        resizable: false,  
        buttons: { 'Ok': function() { (this).dialog("close"); } }  
    });  
});

Unfortunately, this global error handler also fires if the user leaves the page before it finishes loading. Here are the steps to reproduce the error:

  1. User visits Page A, which includes elements that load via AJAX.
  2. AJAX elements on Page A begin loading.
  3. User clicks link to visit Page B before AJAX elements on Page A have finished loading.
  4. The error dialog box appears briefly before the browser redirects to Page B.

Any idea how we can get ajaxError() not to trigger when the error is directly caused by the user visiting a new page?

UPDATE: Here's my code now, after incorporating the suggestions in the comments:

// I added a 3 second delay to our error dialog, enough time
// for the user to leave for a new page:
$(document).ajaxError(function() {
    setTimeout(my_error_handler, 3000);
});

// Warn user before leaving page if AJAX is still loading.
// Not sure I'll keep this:
$(document).ajaxStart(function() {
    ajaxing = true;
});

$(document).ajaxStop(function() {
    ajaxing = false;
});

window.onbeforeunload = function() {
    if (typeof(ajaxing) != 'undefined' && ajaxing) {
        return "Page elements are still loading!";
    }
};

解决方案

Well yes, if the user navigates to another page, the xhr will abort, which is, in effect, an unsuccessful communication. A solution would be to register a listener for the onbeforeunload event, and unbind ajaxError there, basically saying: the user is about to leave the page, so i do not wish to be informed of the ajax errors that might follow.

Some sites, though, like GMail, do the other way around: if you try to navigate away from the page while there are still some ajax requests pending, it gives you an prompt that says you've got some processes still running, and lets the user decide whether to stay on the site until the request has finished, or to go ahead and navigate away.

这篇关于jQuery的ajaxError()处理程序便会启动,如果用户离开网页页面加载完毕前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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