网上找了好多实现这个的方法,说的还是不明白。害得我(我自己的原因)以为是需要在服务器环境下才能跑通 window.onbeforeunload; 后来猜想是不是函数返回值发生变化就会触发。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<body>
<input type="text" id = 'input' value = 'kkk'>
</body>
<script>

window.onbeforeunload = function() { 
    return $('#input').val()
}
</script>
</html>

 以上仅仅是我个人的见解,没有得到认证,项目中使用的时候还有些问题。所以大家辩证的看 以后得到验证后再更新。

---------以下08-22更新------

 

window.onbeforeunload =function() {
   // code...
return null; }
如果返回null的话,就不会弹出对话框,在这个函数中可以进行操作。改变弹出框文字的没找到,应该是不可以的。(https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Notes)

 

相关文章:

  • 2021-05-20
  • 2021-05-06
  • 2021-07-12
  • 2021-06-23
  • 2022-12-23
  • 2021-08-23
  • 2021-07-20
猜你喜欢
  • 2022-12-23
  • 2021-10-08
  • 2022-01-05
  • 2021-04-24
  • 2022-12-23
  • 2021-10-13
  • 2021-10-17
相关资源
相似解决方案