且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在IE 7和& ;;中使用OnBeforeUnload事件的问题8 - 提示消息两次

更新时间:2023-02-05 20:31:30

您好,


尝试


....


        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.