无法在 Firefox 4 中触发卸载事件

发布于 2024-11-03 03:37:24 字数 700 浏览 5 评论 0原文

我已经在卸载事件上运行了 ajax 调用大约一年了。 它通常在 FF 和 IE 中工作,但不是 100%,我不能说它何时失败。 我通过在 bodytag 中写入来注册事件:
onunload =“....”
我在 FF4 中收到错误消息,因为卸载事件还想写入刚刚卸载的页面的 div 标签。如果目标 div 的 id 为“虚拟”,则通过使 ajax 例程不写入任何内容来修复此问题 我不是 AJAX 专家,但以下代码有效: http://yorabbit.info/e-dog.info/tmp/ajax_ex.php (链接是文本页面)
(您使用以下参数调用 ajaxfunction2:文件名、PHP 的查询字符串、更新期间在目标 div 中显示的字符串、目标 div 的名称)

我在 FF 错误控制台中没有收到任何错误消息,并且 IE9 可以正常工作。 有什么方法可以让它在FF中也能工作吗?我刚刚开始尝试FF4,但我的印象是它的工作效果不如FF3。

谢谢。

(我正在旅行,无法立即回复,但我非常感谢建议,并将在适当的时候回复)

编辑: 我最好加上这个: 我在卸载时进行的 AJAX 调用仅将一些数据(用户在页面上停留的时间)发送到 PHP-MySQL 服务器

I have run ajax-calls on the unload event for about a year.
It has generally worked in FF and IE but not to 100%, I cannot say when it has failed.
I register the event by writing in the bodytag:
onunload="...."
I got error messages in FF4 since the unload event also wanted to write in a div-tag of the page that just had unloaded. Fixed this by making the ajax-routine write nothing if the id of the target div is 'dummy'
I am no expert on AJAX, but the following code has worked:
http://yorabbit.info/e-dog.info/tmp/ajax_ex.php (the link is a text-page)
(You call ajaxfunction2 with the following arguments: filename, queryString for PHP, string to show in target div during update, name of target div)

I don't get any error messages in the FF error console and IE9 works. Is there any way I can make it work in FF too?? I have just started trying FF4, but my impression is that it works less well than in FF3.

Thanks.

(I am on a trip and ay not have the possibility to reply immediately, but I really appreciate suggestions and will reply in due course)

EDIT:
I had bettter add this:
The AJAX-call I make on unload does only send some data (how long time the user stayed on the page) to the PHP-MySQL server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-11-10 03:37:24

我不知道这里发生了什么,但是 Firefox 4 对卸载的工作方式进行了显着的更改:例如,如果您在链接单击事件期间执行alert(),它将不再冻结页面,而是加载新的页面无论如何,位置。也许这就是类似的东西。

但是,如果 Ajax 调用在任何浏览器中都不同步,则永远无法保证 Ajax 调用能够完成,无论如何 - 在页面关闭之前,请求可能会也可能不会返回响应。这是否有效将取决于机会以及用户的网络速度。

首先尝试使用同步请求,如下所述: jQuery 的同步 AJAX 请求如何工作?

这通常可以保证请求返回。但是,请非常谨慎使用它 - 页面卸载时的阻塞行为可能会让用户非常恼火,甚至会冻结浏览器。

I don't know what is happening here, but Firefox 4 has made notable changes to how unloading works: For example, if you do an alert() during a link click event, it will no longer freeze the page, but load the new location anyway. Maybe this is something similar.

However, you are never guaranteed for the Ajax call to finish if it is not synchronous in any browser anyway - the request may or may not come back with a response until the page has been closed. Whether this works will be down to chance, and the user's network speed.

Try using a synchronous request first, as outlined here: How does jQuery's synchronous AJAX request work?

this will usually guarantee that the request comes back. However, use it very sparingly - blocking behaviour at page unload can be very annoying for the user, and even freeze the browser.

花辞树 2024-11-10 03:37:24

我建议使用 jQuery,而不是自己跟踪浏览器的更改。

解决方案:
在这里找到工作示例: http://jsfiddle.net/ezmilhouse/4PMcc/1/

假设您的内部链接是相对设置的,因此您的外部链接设置以“http”开头:

<a href="http://google.com">Leave ...</a>
<a href="/home.html">Stay ...</a>

您可以通过 jQuery 事件劫持“a”标签并要求用户确认离开(如果是外部链接)。在“ok”的情况下,您启动“onleave”ajax 调用(async=true)并将用户重定向到外部链接:

$('a').live('click', function(event){

    // cache link
    var link = $(this).attr('href');

    // check if external link (assuming that internal links are relative)
    if (link.substr(0,4) === "http") {

        // prevent default a tag event
        event.preventDefault();

        // popup confirm message
        var reply = confirm('Do you really want to leave?');
        if (reply) {

            var url = 'http:mydomain.com/ajax.php';
            var data = {'foo': 'bar', 'fee':'bo'};

            // kick off your 'onleave' ajax call
            // forced to be synchronous
            $.ajax({ 
                type: "POST",
                async: false,
                url: url,
                data: data,
                success: function( data ) { 

                   // ok case: leave page, cached link
                   window.location.href = link;

                }
            });

        }

        return false;

    }

});

I suggest to use jQuery instead of keeping track of browser changes yourself.

Solution:
Find working sample here: http://jsfiddle.net/ezmilhouse/4PMcc/1/

Assuming that your internal links are set relatively, and your external links therefore set starting with 'http':

<a href="http://google.com">Leave ...</a>
<a href="/home.html">Stay ...</a>

You could hijack 'a' tags via jQuery events and ask the user to confirm the leaving (in case of external links). In 'ok' case you kick off your 'onleave' ajax call (async=true) and redirect user to external link:

$('a').live('click', function(event){

    // cache link
    var link = $(this).attr('href');

    // check if external link (assuming that internal links are relative)
    if (link.substr(0,4) === "http") {

        // prevent default a tag event
        event.preventDefault();

        // popup confirm message
        var reply = confirm('Do you really want to leave?');
        if (reply) {

            var url = 'http:mydomain.com/ajax.php';
            var data = {'foo': 'bar', 'fee':'bo'};

            // kick off your 'onleave' ajax call
            // forced to be synchronous
            $.ajax({ 
                type: "POST",
                async: false,
                url: url,
                data: data,
                success: function( data ) { 

                   // ok case: leave page, cached link
                   window.location.href = link;

                }
            });

        }

        return false;

    }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文