【问题标题】:How to prompt the user before closing the browser/tab?如何在关闭浏览器/选项卡之前提示用户?
【发布时间】:2014-08-20 12:58:34
【问题描述】:
var isRefresh = false;
$(document).on("keydown", function(e) {
    if (e.which === 116 || e.which == 17 || e.which == 1)
    {
      isRefresh = true;
    }
});
$(window).bind('click', function(event) {
    if(event.target.href || event.which == 1) 
        $(window).unbind('beforeunload');
});
$(window).bind('beforeunload', function(event) {
  if(!isRefresh){
   var e = window.event;
     if (e) {
    e.returnValue = 'Are you sure?';
  }

  // For Safari
  if (navigator.appVersion.indexOf("Chrome/") != -1){
  return  '';
  }
  else{
    return 'Are you sure?'
  }
 }
});

此脚本用于关闭选项卡,F5。根据我的要求,在按下浏览器 URL 时,浏览器的后退按钮不应出现提示。整体提示应仅在关闭选项卡时出现。

【问题讨论】:

    标签: browser


    【解决方案1】:

    首先,您无法使用网页/应用程序中的 Javascript 控制浏览器的行为。

    虽然refresh,closing 行为浏览器考虑了网页状态的变化,这对于 Javascript 来说是相同的,因此它显示相同的消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 2013-03-14
      • 1970-01-01
      • 2015-12-27
      • 2018-03-29
      • 2011-01-05
      相关资源
      最近更新 更多