Javascript:禁止显示“此页面要求您确认要离开"在onbeforeunload上弹出 [英] Javascript: suppress "This page is asking you to confirm that you want to leave" popup on onbeforeunload

查看:43
本文介绍了Javascript:禁止显示“此页面要求您确认要离开"在onbeforeunload上弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户离开页面时,我需要问他是否要在离开之前执行特定操作.

When a user leaves a page, I need to ask him if he wants to perform a particular action before leaving.

<script type="text/javascript">
$(document).ready(function() {
        window.onbeforeunload = askConfirm;
});

function askConfirm() {
    var addFriend = confirm("Would you like to ping?");
    if (addFriend) {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        } else {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET","http://example.com?ping=1234",true);
        xmlhttp.send();
    }
    return null;
}

取决于最后一个 return 语句(" null true false ,或者根本没有 return 语句)我可以有以下两种情况之一:

Depending on the value of that last return statement ("", null, true, false, or not having a return statement at all) I can have one of two situations:

1)用户收到您想ping吗?"确认(好),发送ping(好),并向用户显示此页面正在要求您确认要离开"弹出窗口(BAD).

1) The user gets the "Would you like to ping?" confirmation (good), the ping is sent (good), and the user is presented with a "This page is asking you to confirm that you want to leave" popup (BAD).

-或-

2)用户收到您要ping吗?"确认(良好), ping 未发送(BAD),并且向用户显示此页面要求您确认您要离开"弹出窗口(好).

2) The user gets the "Would you like to ping?" confirmation (good), the ping is not sent (BAD), and the user is not presented with a "This page is asking you to confirm that you want to leave" popup (good).

如何发送AJAX ping,但不显示此页面正在要求您确认要离开"弹出窗口?

听起来很荒谬,我为此问题发现的解决方法"是在 xmlhttp.send()语句.唯一干净的方法是提醒用户其ping已发送.如果将来的StackOverflowers找到更好的解决方案,我很想知道.

As ridiculous as it sounds, the "work around" that I found for this issue is to alert() after the xmlhttp.send() statement. The only clean way to do that is to alert the user that his ping has been sent. If future StackOverflowers find a better solution, I would love to know.

谢谢.

推荐答案

好,而不是在您离开之前这样做,我在onunload上完成了,这就是我在FF和IE中工作的地方(除了chrome,这就是我得到的全部): http://jsfiddle.net/rmpLm/9/.

well instead of doing it before you leave, I did it onunload and here is what I got to work in FF and IE (besides chrome, that's all I got): http://jsfiddle.net/rmpLm/9/.

也许可以在chrome上使用它,但是如果不能,也许可以这样做,以便如果单击ok,它将带您到发送它的页面.希望对您有所帮助:)

perhaps you can get it to work on chrome, but if you cant, maybe you could make it so that if you hit ok, it takes you to a page where you send it. hope it helps : )

也许您应该考虑这样做,这样它才能在加载时完成

maybe you should consider making it so it does it onload

这篇关于Javascript:禁止显示“此页面要求您确认要离开"在onbeforeunload上弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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