When browser is closed i want to call the web method of the webservice .. How can be done.??

Discussion in 'ASP .Net' started by Gouri.Mahajan7@gmail.com, Jun 11, 2008.

  1. Guest

    Hello,

    Can anybody please tell me how to trap the browser close event. when
    that is fired I want to call the web method.

    Thanks in advance.

    Regards,
    Gouri.
     
    , Jun 11, 2008
    #1
    1. Advertising

  2. subtile Guest

    Re: When browser is closed i want to call the web method of the we

    would it be possible with som javascript.. calling a webservice ?
    --

    Jesus Loves You


    "Mark Rae [MVP]" wrote:

    > <> wrote in message
    > news:...
    >
    > > Can anybody please tell me how to trap the browser close event. when
    > > that is fired I want to call the web method.

    >
    > This is a fairly common question in this newsgroup.
    >
    > Unfortunately, there is no reliable way to do this, because there is no
    > permanent connection between the webserver and the client browser. The
    > webserver has no idea what happens on the client browser until / unless it
    > receives another HttpRequest.
    >
    > No doubt you'll receive other replies suggesting possible solutions with the
    > unload and/or beforeunload events - none of those works reliably...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
    >
     
    subtile, Jun 11, 2008
    #2
    1. Advertising

  3. Re: When browser is closed i want to call the web method of the we

    Hello subtile,

    Yep, you can use onbeforeunload of the body < body onbeforeunload="BrowerClose();">
    and make ajax call to the web-service http://www.asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

    it's a common practice for this

    ---
    WBR,
    Michael Nemtsev [Microsoft MVP] :: blog: http://spaces.live.com/laflour

    "The greatest danger for most of us is not that our aim is too high and we
    miss it, but that it is too low and we reach it" (c) Michelangelo


    s> would it be possible with som javascript.. calling a webservice ?
    s>
    s> "Mark Rae [MVP]" wrote:
    s>
    >> <> wrote in message
    >> news:
    >> ...
    >>
    >>> Can anybody please tell me how to trap the browser close event. when
    >>> that is fired I want to call the web method.
    >>>

    >> This is a fairly common question in this newsgroup.
    >>
    >> Unfortunately, there is no reliable way to do this, because there is
    >> no permanent connection between the webserver and the client browser.
    >> The webserver has no idea what happens on the client browser until /
    >> unless it receives another HttpRequest.
    >>
    >> No doubt you'll receive other replies suggesting possible solutions
    >> with the unload and/or beforeunload events - none of those works
    >> reliably...
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.ne
     
    Michael Nemtsev [MVP], Jun 11, 2008
    #3
  4. Munna Guest

    Re: When browser is closed i want to call the web method of the we

    On Jun 11, 7:53 pm, "Mark Rae [MVP]" <> wrote:
    > "subtile" <> wrote in message
    >
    > news:...
    >
    > [top-posting corrected]
    >
    > > would it be possible with som javascript.. calling a webservice ?

    >
    > Not reliably... The JavaScript would fire every time the page closes i.e.
    > while moving from page to page within the same web app, and also during
    > postback etc...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    Hi,

    what Mark is said is absolutely true and i found a little workaround
    to that...

    here is the code i tried and found working on both IE and Firefox...

    <body onbeforeunload="doUnload()" onmousedown="somefunction()">
    <form id="form1" runat="server">
    </form>
    </body>
    <script language="javascript" type="text/javascript">
    var isClose = false;
    function somefunction()
    {
    isClose = true;
    }
    function doUnload()
    {
    if(!isClose)
    {
    alert('window is closing');
    }
    }
    </script>

    this code is not perfect ... you guys can find some flaw ... still it
    works... for most of the cases...

    Best of luck

    Munna

    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
     
    Munna, Jun 11, 2008
    #4
  5. bruce barker Guest

    Re: When browser is closed i want to call the web method of the we

    this code fires on every page navigation (event postback) not just browser
    close (though it will also fire on browser close).

    this pattern is usually used to detect navigating away with unsaved data.


    -- bruce (sqlwork.com)


    "Munna" wrote:

    > On Jun 11, 7:53 pm, "Mark Rae [MVP]" <> wrote:
    > > "subtile" <> wrote in message
    > >
    > > news:...
    > >
    > > [top-posting corrected]
    > >
    > > > would it be possible with som javascript.. calling a webservice ?

    > >
    > > Not reliably... The JavaScript would fire every time the page closes i.e.
    > > while moving from page to page within the same web app, and also during
    > > postback etc...
    > >
    > > --
    > > Mark Rae
    > > ASP.NET MVPhttp://www.markrae.net

    >
    > Hi,
    >
    > what Mark is said is absolutely true and i found a little workaround
    > to that...
    >
    > here is the code i tried and found working on both IE and Firefox...
    >
    > <body onbeforeunload="doUnload()" onmousedown="somefunction()">
    > <form id="form1" runat="server">
    > </form>
    > </body>
    > <script language="javascript" type="text/javascript">
    > var isClose = false;
    > function somefunction()
    > {
    > isClose = true;
    > }
    > function doUnload()
    > {
    > if(!isClose)
    > {
    > alert('window is closing');
    > }
    > }
    > </script>
    >
    > this code is not perfect ... you guys can find some flaw ... still it
    > works... for most of the cases...
    >
    > Best of luck
    >
    > Munna
    >
    > www.munna.shatkotha.com
    > www.munna.shatkotha.com/blog
    >
     
    bruce barker, Jun 11, 2008
    #5
  6. Munna Guest

    Re: When browser is closed i want to call the web method of the we

    Hi bruce

    Thanks for your reply i checked the code again after your replay

    did you actually run the code i suggesed...?

    i tested this code and found working...

    the trick is simple

    when mouse down in browser document traked it down.

    Thanks

    Munna
     
    Munna, Jun 11, 2008
    #6
  7. Munna Guest

    Re: When browser is closed i want to call the web method of the we

    Hi

    well may be the code need a little explanation...

    the event onbeforeunload will fire each and every time browser
    navigate,postback,close...
    as bruce said is right...

    but i have subscribed another code that is mousedown of the body...

    each time the user do a operation using mouse in the document i
    tracked it down in a variable

    if a use close its browser there is no mouse down in document... thats
    why i used this code...

    <body onbeforeunload="doUnload()" onmousedown="somefunction()">
    <form id="form1" runat="server">
    </form>
    </body>
    <script language="javascript" type="text/javascript">
    var isClose = false;
    function somefunction()
    {
    isClose = true;
    }
    function doUnload()
    {
    if(!isClose)
    {
    alert('window is closing');
    }
    }
    </script>

    we can enhance this code ofcourse...
    adding key press event to track down alt+f4 and etc...


    Thanks

    Munna
     
    Munna, Jun 11, 2008
    #7
  8. Munna Guest

    Re: When browser is closed i want to call the web method of the we

    On Jun 11, 9:47 pm, "Mark Rae [MVP]" <> wrote:
    > "Munna" <> wrote in message
    >
    > news:...
    >
    > > when mouse down in browser document traked it down.

    >
    > Alt-{F4}
    > Alt F X
    > Alt-{LEFT} / Alt-{RIGHT}
    > Ctrl-{F5}
    >
    > Etc...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    Hi

    Nice points by Mark...

    well here is a simple javascript function that can be used to handle
    key events...

    this is just an example...

    <body onbeforeunload="doUnload()" onmousedown="somefunction()">
    <form id="form1" runat="server">
    </form>
    </body>
    <script language="javascript" type="text/javascript">
    var isClose = false;

    //this code will handle the F5 or Ctrl+F5 key
    document.onkeydown = checkKeycode
    function checkKeycode(e) {
    var keycode;
    if (window.event)
    keycode = window.event.keyCode;
    else if (e)
    keycode = e.which;

    if(keycode == 116)
    {
    isClose = true;
    }
    }

    function somefunction()
    {
    isClose = true;
    }

    function doUnload()
    {
    if(!isClose)
    {
    alert('window is closing');
    }
    }
    </script>


    Thanks

    Munna
     
    Munna, Jun 11, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Techsatish
    Replies:
    1
    Views:
    315
    Egghead
    Sep 22, 2006
  2. ESPNSTI
    Replies:
    0
    Views:
    133
    ESPNSTI
    May 9, 2007
  3. René Preußler

    Asynchronous Call of WebService method within a WebService

    René Preußler, Oct 29, 2008, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    198
    René Preußler
    Oct 29, 2008
  4. Matt Kruse
    Replies:
    5
    Views:
    311
    Richard Cornford
    Sep 9, 2003
  5. Zhidian Du
    Replies:
    0
    Views:
    158
    Zhidian Du
    Feb 21, 2004
Loading...

Share This Page