How to know if user close the browser or navigate to another page ?

Last post 02-27-2008 8:45 AM by HarveyTriana. 2 replies.

Sort Posts:

  • How to know if user close the browser or navigate to another page ?

    01-15-2008, 11:14 AM
    • Member
      57 point Member
    • HarveyTriana
    • Member since 01-08-2008, 11:05 AM
    • Bogotá / Colombia
    • Posts 20

    I know... need and event from client script to server...

    Somebody has write this code?

    Thanks 

     

    Harvey Triana
    http://vexpert.mvps.org
  • Re: How to know if user close the browser or navigate to another page ?

    01-21-2008, 4:38 AM
    Answer

    Hi,

    Thank you for your post!

    There are two ways to do this:

    1.using window.onbeforeunload() event, do it like this:

    function  window.onbeforeunload()  {  alert("close the window?")}

    or

    function  window.onbeforeunload()  { 
     if  (event.clientX>document.body.clientWidth  &&  event.clientY<0 ||event.altKey) 
         window.event.returnValue="Are you sure?";
     }

    2.using onUnload event

    body onUnload="myClose()"

    If you have further questions, let me know! 

    Best Regards,

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
  • Re: How to know if user close the browser or navigate to another page ?

    02-27-2008, 8:45 AM
    • Member
      57 point Member
    • HarveyTriana
    • Member since 01-08-2008, 11:05 AM
    • Bogot&amp;#225; / Colombia
    • Posts 20

    Hi-

    Thanks a lot for your help. I did know the client side code. I wish complement reply with the server side code.

    My original question is "How to know if user close the browser or navigate to another page ?"... missing: "I need run something in backend".
    Right, to send an event to server side from client, we need a callback. I am working an approach that’s works fine:

    Simple sample:
    ...
    using System.IO;
    ...
    public partial class test : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;
            string er = ClientScript.GetCallbackEventReference(this, "", "null", "null", "null", true);
            string rb = "function callServerTask(arg) {" + er + ";}";
            cs.RegisterClientScriptBlock(this.GetType(), "callServerTask", rb, true);
            // body has attribute rutat=server id=body
            body.Attributes.Add("onunload", "callServerTask('');return false;");
        }
        public void RaiseCallbackEvent(string eventArgument)
        {
            CloseMe();
        }
        public string GetCallbackResult()
        {
            return "";
        }
        private void CloseMe()
        {
            // to test that callback works
            StreamWriter s = new StreamWriter(Server.MapPath("closehistory.txt"), true);
            s.WriteLine("close at:" + DateTime.Now.ToString());
            s.Close();
        }
    }

    Warning: In IDE of Visual Studio works when navigate to another page. In production, works always.

    Regards.

     

     

     

     

    Harvey Triana
    http://vexpert.mvps.org
    Filed under:
Page 1 of 1 (3 items)