使用onbeforeunload没有对话框? [英] Using onbeforeunload without dialog?

查看:156
本文介绍了使用onbeforeunload没有对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户离开我的网页时发布数据。我终于找到了一个可行的解决方案,但是,当用户离开时,它会显示一个确认对话框。我试过 return null; 但它没有用。是否可以禁用对话框?

I'm trying to post data when a user leaves my page. I have finally managed to find a working solution, however, it shows a confirmation dialog when the user leaves. I have tried return null; but it didn't work. Is it possible to disable the dialog?

window.onbeforeunload = function() {
    $.post("track.php", {
        async: false,
        refid: refid,
        country: country, 
        type: type,
    });

    return '';
}


推荐答案

来自Mozilla开发者网络页面


当此事件返回非void值时,系统会提示用户
确认页面卸载。

When this event returns a non-void value, the user is prompted to confirm the page unload.

这意味着处理程序的返回值必须是 undefined (不是'' false null ),以避免触发确认提示。

This means the return value of the handler must be undefined (not '', false, or null) in order to avoid triggering the confirmation prompt.

window.onbeforeunload = function() {

  $.post("track.php", {
  ...
  });

  return undefined;
}

在javascript中,您可以完全跳过返回值以获得相同的结果。

In javascript you can skip the return value altogether to get the same result.

在带有jquery的coffeescript中,它类似于

In coffeescript with jquery it's something like

$(document).ready ->
  $(window).bind('beforeunload', ->
    #put your cleanup code here
    undefined
  )

这篇关于使用onbeforeunload没有对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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