在IE 7和&amp ;;中使用OnBeforeUnload事件的问题8 - 提示消息两次 [英] Problem using OnBeforeUnload event with IE 7 & 8 - prompting message twice

查看:77
本文介绍了在IE 7和&amp ;;中使用OnBeforeUnload事件的问题8 - 提示消息两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发asp.net网站,我要求如果用户离开网页而没有保存值,我需要提示一条消息,询问"你想要退出页面而不保存任何更改吗? ",


如果用户按下"是",我需要触发服务器端(代码隐藏),之后需要关闭浏览器,


如果用户按下"否​​",请不要关闭页面并保持原样打开。


我正在使用此javascript函数。我在我的控件"OnChange"上设置了更改的值变量。事件。

< script type =" text / javascript"语言= QUOT; JavaScript的"> 

var isDirty;
isDirty = 0;

函数setDirty(){
isDirty = 1;
}

function checkSave(){
var sSave;
if(isDirty == 1){
sSave = window.confirm("你有一些尚未保存的更改。点击OK立即保存或CANCEL继续而不保存。");
if(sSave == true){
//document.getElementById('__EVENTTARGET').value ='Button1';
//document.getElementById('__EVENTARGUMENT').value ='点击';
document.getElementById('<%= Button3.ClientID%>')。click();
//window.document.form1.submit();
// __ doPostBack('Button1','OnClick');

}其他{
返回true;
}
}
}


window.onbeforeunload = checkSave;


< / script>

但上面代码中的问题是,在提示我的消息后,IE再次提示另一条消息"你确定要离开这个页面吗?"然后我无法在是或否的情况下触发我的事件。


所以我在找什么是如何捕获浏览器关闭事件,显示带有是/否选项的消息并依赖是/否值,可以采取措施吗?


另外,请记住用户可以通过单击关闭浏览器on"Close"按钮位于顶部或按Alt + F4,无论如何,我需要捕捉浏览器关闭。


 


Nirav

解决方案

您好,


尝试


....


        document.getElementById('<%= Button3.ClientID%>')。click();

     ;&NBSP;&NBSP;&NBSP;&NBSP; // window.document.form1.submit();

        // __ doPostBack('Button1','OnClick');


返回false; ////在此添加.......


      } else {


 


问候。


Hi, I am developing asp.net website and I have a requirement that if user leaves a webpage without saving values, I need to prompt a message asking "Do you want to exit page without saving any changes?",

and if user presses "Yes", I need to fire a server side ( code behind ) and need to close Browser after that,

and if user presses "No", do not close the page and keep it opened as it is.

I am using this javascript function. I am setting changed value variable on my controls "OnChange" event.

<script type="text/javascript" language="javascript"> 

var isDirty; 
  isDirty = 0; 
 
  function setDirty() { 
    isDirty = 1; 
  } 
 
  function checkSave() { 
    var sSave; 
    if (isDirty == 1) { 
      sSave = window.confirm("You have some changes that have not been saved. Click OK to save now or CANCEL to continue without saving."); 
      if (sSave == true) {
        //document.getElementById('__EVENTTARGET').value = 'Button1';
        //document.getElementById('__EVENTARGUMENT').value = 'Click';
        document.getElementById('<%=Button3.ClientID%>').click();
        //window.document.form1.submit();
        //__doPostBack('Button1', 'OnClick');

      } else { 
         return true; 
      } 
    } 
  }


  window.onbeforeunload = checkSave;


 </script>

But the problem in above code is that after prompting my message, again IE is prompting another message "Are you sure you want to navigate away from this page?", and then I am not able to get my events fired upon yes or no.

So what I am looking for is How can I capture Browser Close event, display a message with yes/no option and depending upon yes/no value, can take action?

Also, remember that user can close browser by clicking on "Close" button at top or by pressing Alt + F4 as well, I need to capture browser close if it gets closed anyway.

 


Nirav

解决方案

Hi,

Try

....

        document.getElementById('<%=Button3.ClientID%>').click();
        //window.document.form1.submit();
        //__doPostBack('Button1', 'OnClick');

return false; //// add this here.....

      } else {

 

Regards.


这篇关于在IE 7和&amp ;;中使用OnBeforeUnload事件的问题8 - 提示消息两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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