2

I have noticed that window.onunload event fires off AFTER page_load event which makes no sense.

This behaviour is creating an issue for me - in my unonload I clear the session, so if the Page_Load first BEFORE onunload, there are errors on the page displayed.

I would expect the javascript onunload to fire BEFORE Page_Load....is that the correct assumption?

TO CLARIFY: Let's assume I am on page test.aspx, then I click on the link that goes to the same page (say I click on a menu), what I observe is that Page_Load fires first, then onunload fires off. Makes no sense at all.

sarsnake
  • 26,667
  • 58
  • 180
  • 286
  • This question does not make a lot of sense as it is currently phrased. Why would you expect the page to unload BEFORE it loads? – EricLaw Mar 02 '10 at 00:16
  • Is there any special reason that you want to clear the session of the user onunload event. – azamsharp Mar 02 '10 at 02:05

6 Answers6

2

It's a browser-specific behaviour. Chrome and FF will send a GET requst BEFORE onunload is fired, IE8 will execute onunload first. Not sure, how the other browser handle it. Better not rely on this functionality.

Dimitri Vorona
  • 450
  • 3
  • 13
1

Have you considered using a common base class for your pages, and clearing the session in there if the request isn't a postback (I assume that you're using session for postbacks)?

public class BasePage : System.Web.UI.WebControls.Page {
  protected override OnPreInit (EventArgs e) {
    // Get in nice and early, however you could use OnInit if you prefer
    if (!Page.IsPostBack) {
      Session.Clear();
    }          
}

Then your pages that need to clear session can be declared as:

public class SpecialPage : BasePage {
  // Your page logic goes here.
  // Note that if you need to do work in OnPreInit here you should call
  // base.OnPreInit(e) first.
}
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
0

I would guess that window.unload is actually firing only when you're going to have to RENDER the new page you navigated to (aka the old DOM is being torn down in place of some new HTML). The browser doesn't know what to render until the response comes back from the server with the HTML to display. That HTML isn't generated until the page lifecycle completes, which includes Page_Load. Hence the page_load before the window.unload?

In any case, if you can clear the session during window.unload, why not just clear it in response to some user interaction and be a bit more explicit about it?

Edit: Could you also try window.onbeforeunload?

Eric
  • 3,284
  • 1
  • 28
  • 29
  • the problem is that i can't control the user. the user may choose to click wherever they want to. so if they click on a menu, i would expect that the window.onunload will fire first, clear the session, and then page_load will fire. not so. – sarsnake Mar 02 '10 at 00:32
  • I edited my answer - does window.onbeforeunload help your cause? – Eric Mar 02 '10 at 00:37
  • Thanks, but onbeforeunload creates more problems actually - it pops the dialogue that's not manually accessible for a programmer. I don't want the dialogue popped up, I just want the session to be cleared, THEN new page loaded. Arggg, apparently too much to ask for. – sarsnake Mar 02 '10 at 00:45
0

The onunload event does fire before the request for the new page is fired off to the server, so it definitely fires before the Page_Load method runs on the server.

The problem is most likely that you are sending another request to the server from the onunload event. As the IIS only handles one request at a time from each user, this request will be queued and executed after the request for the new page.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I commented out the request to the server inside onunload, put break points inside Page_Load. Still, Page_Load executes first, then alert from window.unonload fires off......what gives? what's the point of window.unonload event then if it executes AFTER the new page has loaded. – sarsnake Mar 02 '10 at 01:04
  • Is there a way to TRULY clear session BEFORE loading the new page? I am totally lost and frustrated. – sarsnake Mar 02 '10 at 01:20
  • @guffa - I just wrote a little test app, and I don't think onunload fires BEFORE the request to the server. I'm skeptical myself - but the test app doesn't lie. – Eric Mar 02 '10 at 01:54
0

You can write an utility function which will handle removing of the session variables and then call that function in the respective menu click events. That should be simpler to use since window unload will fire after page load only.

Abhishek Rao
  • 93
  • 1
  • 3
  • 13
0

It sounds like you are using the Session to save temporary variables that change from page to page. I would say the Session is not really suitable for this kind of scenario. A better solution would be to use the Httpcontext's Item collection which is scoped only on a per request basis. It's works just the same as the Session when storing data.

Context.Items["myvariable"] = "some data";

As it's only scoped on a per request basis, there is no need to use javascript to clear the items you have stored on each page request.