本文介绍了使用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 '';
}


推荐答案

来自:

这意味着处理程序的返回值必须是 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没有对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:26