locked
Asp .Net : Detect browser closing/page change RRS feed

  • Question

  • User1163317553 posted

    So far I am using:

     

    1    protected void Page_Init(object sender, EventArgs e) 
    2    {
    3    
    4    this.Unload += new EventHandler(QuickQuote_Unload); 
    5    }
    6    
    7    protected void QuickQuote_Unload(object sender, EventArgs e) 
    8    {
    9    
    10       (Stored Procedure) 
    11   
    12   }
    13   
    
     

     The stored procedure adds data to the DB, but it does it on page load instead of unload...  I am basically trying to capture the fields that the user has entered information in to when they leave the page without hitting submit.  If anyone has any ideas on how to do this please let me know...

    Monday, December 3, 2007 2:32 PM

Answers

  • User807886609 posted

    You cannot detect when a browser is closed (not reliably, and certainly not with the server-side code that you show). The only time you can detect that something is happening on the browser end is when a new request comes in from that browser. Once generated, the page class is gone forever, and the results (HTML, etc) are sitting in the browser being displayed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 3, 2007 4:32 PM
  • User807886609 posted

    This article

    http://www.eggheadcafe.com/articles/20010406.asp

    Discusses the use of the onBeforeUnload client - script method to warn the user if their form hasn't been submitted (and also save what data they entered, so they can come back and complete it).

    That's about the closest you can get to what you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2007 10:18 AM
  • User188494879 posted

     You can alert when someone tries to close the browser using :

    <script type="text/javascript">
    
    function closeit()
    {
         event.returnValue = "Closit";
    }
    
    </script>
    
    Call this on the body as:
    <body onbeforeunload="closeit()">
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2007 10:45 AM

All replies

  • User-1738395370 posted

    Why dont you try and search for the answer on google? Or you can try buying on of Scott Gu's books online or go to joeon.net and read his blog!

    Monday, December 3, 2007 3:20 PM
  • User807886609 posted

    You cannot detect when a browser is closed (not reliably, and certainly not with the server-side code that you show). The only time you can detect that something is happening on the browser end is when a new request comes in from that browser. Once generated, the page class is gone forever, and the results (HTML, etc) are sitting in the browser being displayed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 3, 2007 4:32 PM
  • User1163317553 posted

    If I could at least get the information when the user navigates to another page it would work for me.  Really the closing of the page does not have to be 100% reliable since I just need an idea of what is being left blank before the user navigates away from the page.

    Tuesday, December 4, 2007 8:14 AM
  • User807886609 posted

    This article

    http://www.eggheadcafe.com/articles/20010406.asp

    Discusses the use of the onBeforeUnload client - script method to warn the user if their form hasn't been submitted (and also save what data they entered, so they can come back and complete it).

    That's about the closest you can get to what you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2007 10:18 AM
  • User188494879 posted

     You can alert when someone tries to close the browser using :

    <script type="text/javascript">
    
    function closeit()
    {
         event.returnValue = "Closit";
    }
    
    </script>
    
    Call this on the body as:
    <body onbeforeunload="closeit()">
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2007 10:45 AM
  • User807886609 posted

    The code you posted will do nothing. You would have to replace the  line

    event.returnValue="Closit";

    with an actual alert, e.g.:

     alert("Are you sure you want to do this?");

    Tuesday, December 4, 2007 11:31 AM
  • User188494879 posted

    That was just a dummy example

    But the above code will still display a confirm box asking " Do you want to navigate away from the page" 

    Tuesday, December 4, 2007 11:40 AM
  • User-1176147176 posted

    This is all rubbish. This onbeforeunload event occurs whenever you navigate away from the page, not just when someone tries to close the browser.

     As for the users 'right' to close the browser when they wish, they do not have a 'right' to use our web applications as they wish. If we decide our system must perform certain tasks before it lets a user quit, so it will be.

    Tuesday, June 2, 2009 10:52 AM
  • User-1718098015 posted

    Its right that user can use the application ther way, but sometimes its important to do such tasks where we are helpless.Anyways I am having the same problem, I just want to save the loggout time for the loggd in user, no matter by what means . There is no porblem when the user clicks the log out button but when the user tries to logg out by closing browser then I need to have some stuff there. I am not saying that I would ask user to go and click log out browser but can grab the event from cleicnt side and call the server side event.

    The script given using onbeforeunload functions works fine with almost all the browsers but the probelm is I will have to write it to all the pages of the application, as it need to be in the body tag.If someone knows how I can eliminate writing it to every page then please help me out my applcaition is too large and does have too many pages..

    However thanks for the script.

    Thanks and Regards

    Nirav Vyas



    Thursday, December 3, 2009 12:29 PM