On exit page event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Larsen [CPH]

    On exit page event

    Hi,

    How do i track when i move away from a page - aka "window.onbefor eunload",
    but on server side.

    If you think of a sample where you have some textbox's and you want to get
    the contents, before moving away from the page, but you don't want to use
    post-back on each textbox. How is that done ??

    At the moment, i'm trying an idea where i use the following script in on
    window.onbefore unload. This gives me a postback where i have the opportunity
    to read from the page before moving away (or if a button is pushed and
    similar stuff).

    function doOnBeforeUnloa d()
    {
    __doPostBack('n othing', '');
    }

    But i am not sure what the consequences are, using this method. Do i break
    something or is it a possible solution ??
    There is one problem though - if using session states, data could be
    overwritten with data from older pages. But that could be solved by writing
    a timestamp to the page.

    So, is postback a solution or are there other ways to do this ??

    Best Regards
    Peter


  • Mark Rae [MVP]

    #2
    Re: On exit page event

    "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
    news:eB4IaCqGJH A.3628@TK2MSFTN GP06.phx.gbl...
    How do i track when i move away from a page - aka "window.onbefor eunload",
    but on server side.
    Reliably, you can't. This is a very common question in web development, and
    the problem is the inherent disconnected architecture of browser-based
    applications.

    Specifically:
    a) a web browser sends an HttpRequest to a web server
    b) the web server processes that HttpRequest and sends back an HttpResponse
    c) the web browser receives the HttpResponse and renders the HTML

    At this point, the web server has no way of knowing what happens until /
    unless it receives another HttpRequest from the web browser because there is
    no permanent connection between web server and web browser. A common analogy
    is a letter in the post; if you send me a letter, you have no further
    knowledge of what happens to that letter once it's left your hand. You can't
    know whether I read it, whether I throw it away or if I've even received it.
    Unless I tell you!
    If you think of a sample where you have some textboxes and you want to get
    the contents before moving away from the page, but you don't want to use
    post-back on each textbox. How is that done ??
    So, is postback a solution or are there other ways to do this ??
    Postback is a possible solution, but bear in mind that the onbeforeunload /
    beforeunload will fire *every time* the page closes...


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Peter Larsen [CPH]

      #3
      Re: On exit page event

      Hi Mark and thanks for your comments.

      The alternative is a popup message on onbeforeunload, but that leaves me
      with other problems (like getting too many popups) !

      Calling __doPostBack from onbeforeunload looks very similar to a normal
      button click (postback) where the control hasn't no OnClick method assigned
      to it. The only difference is that the event also will be fired when you
      close the tab or browse to a new page.

      To me it sounds like a win-win - except that i should be more careful about
      not getting newer data destroyed by older data.
      Postback is a possible solution, but bear in mind that the onbeforeunload
      / beforeunload will fire *every time* the page closes...
      What do you mean about "every time" ??

      BR
      Peter



      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
      news:%239SEnhqG JHA.3460@TK2MSF TNGP04.phx.gbl. ..
      "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
      news:eB4IaCqGJH A.3628@TK2MSFTN GP06.phx.gbl...
      >

      Comment

      • Mark Rae [MVP]

        #4
        Re: On exit page event

        "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
        news:%23j9SKKrG JHA.4760@TK2MSF TNGP05.phx.gbl. ..
        Calling __doPostBack from onbeforeunload looks very similar to a normal
        button click (postback) where the control hasn't no OnClick method
        assigned to it. The only difference is that the event also will be fired
        when you close the tab or browse to a new page.
        That's right - extremely irritating...
        >Postback is a possible solution, but bear in mind that the onbeforeunload
        >/ beforeunload will fire *every time* the page closes...
        >
        What do you mean about "every time" ??
        Exactly what I say... Every time, not necessarily just when you want it, but
        every time...


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Peter Larsen [CPH]

          #5
          Re: On exit page event

          Hi Mark,

          Do you know of a better way to accomplish this (save data before exit) ??

          /Peter

          "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
          news:Oj3qUbrGJH A.2236@TK2MSFTN GP05.phx.gbl...
          "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
          news:%23j9SKKrG JHA.4760@TK2MSF TNGP05.phx.gbl. ..
          >
          >Calling __doPostBack from onbeforeunload looks very similar to a normal
          >button click (postback) where the control hasn't no OnClick method
          >assigned to it. The only difference is that the event also will be fired
          >when you close the tab or browse to a new page.
          >
          That's right - extremely irritating...
          >
          >>Postback is a possible solution, but bear in mind that the
          >>onbeforeunloa d / beforeunload will fire *every time* the page closes...
          >>
          >What do you mean about "every time" ??
          >
          Exactly what I say... Every time, not necessarily just when you want it,
          but every time...
          >
          >
          --
          Mark Rae
          ASP.NET MVP
          http://www.markrae.net

          Comment

          • Mark Rae [MVP]

            #6
            Re: On exit page event

            "Peter Larsen [CPH]" <PeterLarsen@co mmunity.nospamw rote in message
            news:Ojp5JWwGJH A.3668@TK2MSFTN GP02.phx.gbl...

            [please don't top-post]
            >>Calling __doPostBack from onbeforeunload looks very similar to a normal
            >>button click (postback) where the control hasn't no OnClick method
            >>assigned to it. The only difference is that the event also will be fired
            >>when you close the tab or browse to a new page.
            >>
            >That's right - extremely irritating...
            >>
            >>>Postback is a possible solution, but bear in mind that the
            >>>onbeforeunlo ad / beforeunload will fire *every time* the page closes...
            >>>
            >>What do you mean about "every time" ??
            >>
            >Exactly what I say... Every time, not necessarily just when you want it,
            >but every time...
            >
            Do you know of a better way to accomplish this (save data before exit) ??
            No.


            --
            Mark Rae
            ASP.NET MVP


            Comment

            Working...