【问题标题】:How can I ask users to confirm before leaving the page on iOS safari? [duplicate]如何在离开 iOS Safari 页面前要求用户确认? [复制]
【发布时间】:2015-07-07 14:37:48
【问题描述】:

我现在有:

window.onbeforeunload = confirmFirst;

function confirmFirst() {   
  return "Leave the page?"; 
}   

这可以提示用户确认他们将在所有主流浏览器上离开页面,iOS Safari 除外。我怎样才能让它工作?

我想实现这一点,因为我的用户在我的网站上输入一些长段落后经常不小心向右滑动(后退按钮)并离开页面。谢谢!

【问题讨论】:

  • 这对于IOS设备来说真的很糟糕。这样一个重要的事件(onunload,pagehide)在离开页面之前跟踪关键信息。 #ios 错误

标签: javascript ios safari onbeforeunload confirm


【解决方案1】:

你的代码现在有什么问题?那应该做的工作...
您可以使用常规 JS 来做到这一点:

window.onbeforeunload = function(){
  return 'Leave the page?';
};

甚至是 jQuery:

$(window).bind('beforeunload', function(){   
  return 'Leave the page?'; 
});

这有用吗?
Is there an alternative method to use onbeforeunload in mobile safari?

【讨论】:

【解决方案2】:

window.onbeforeunload = confirmFirst;

function confirmFirst() {   
  if(confirm("Leave the page?")){
    // some code
  }else{
   return false;
  } 
} 

【讨论】:

  • 随机的 jQuery 引用是怎么回事?
猜你喜欢
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
相关资源
最近更新 更多