jQuery .on(“unload”,fn)不适合我

时间:2016-08-05 00:10:18

标签: jquery

我正在尝试检测用户何时离开页面。我认为.on(“unload”,fn)方法应该这样做:

$(document).ready(function(){
  $(window).on("unload", function(){
    alert("Goodbye!");
  });
});

但它似乎不起作用。当我离开页面时没有任何反应。但是,如果我将“卸载”更改为“加载”,则会在页面加载时正确显示警告框。

这是做我想要的正确方法吗?

1 个答案:

答案 0 :(得分:1)

也许:

dfclean['weightedavg'] = dfclean['Star_Rating'] * dfclean['Review_Count'] / dfclean['Review_Count'].sum()

取自http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

" beforeunload"将是一个比'卸载"更可靠的活动。一,似乎已被弃用。我也读过" beforeunload"从javascript调用事件比从jQuery调用时更可靠

所以在你的情况下:

<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }
</script>