如何在window.onbeforeunload中禁用对话框警报并关闭网站?

时间:2016-12-06 08:05:32

标签: javascript onbeforeunload

如何禁用对话框提醒并关闭window.onbeforeunload中的网站?

window.onbeforeunload = function() {
  var isVisible = $('#submitbtn').is(':visible');
  if(isVisible==true){
    return null;
  }else{
    return 'you havent close';
  }

1 个答案:

答案 0 :(得分:1)

return undefined;以阻止window.onbeforeunload

期间的对话

<强>演示: -

&#13;
&#13;
window.onbeforeunload = function(e) {
  var isVisible = $('#submitbtn').is(':visible');
  if (isVisible == true) {
    $(window).unbind();
    return undefined;
    //is there any way to disable dialog alert and close the website??
  } else {
    return 'you havent close';
  }
};
$('#hidebtn').on("click",function(){
  $('#submitbtn').toggle();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="httpp://google.com">Leave page</a>
<input type="button" id="submitbtn" value="You will not get the dialog untill i am visible" />
<input type="button" id="hidebtn" value="hide/show" />
&#13;
&#13;
&#13;