Jquery阻止窗口关闭 [英] Jquery prevent window closing

查看:198
本文介绍了Jquery阻止窗口关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想询问用户是否真的要离开页面,当他点击关闭按钮时就像在Google文档中完成一样。如何使用jquery?

I want to ask user whether he really want to leave the page or not, when he clicks the close button just like it done in google docs. How to do that using jquery?

推荐答案

你设置窗口的 onbeforeunload 属性到函数。

You set the window's onbeforeunload property to a function.

这篇文章就如何做到这一点有一个很好的例子。

或者另一个例子:

<script language="JavaScript">
  var needToConfirm = true;

  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm)
      //return message to display in dialog box;
  }
</script>

...

<input type="Submit" value="Save" onclick="needToConfirm = false;" />



设置 needToConfirm 您的表格可以:

$(document).ready(function() { 
    $(':input', document.myForm).bind("change", function() { needToConfirm = true; }); // Prevent accidental navigation away
});

这篇关于Jquery阻止窗口关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆