Javascript window.onunload fires off after Page_Load

℡╲_俬逩灬. 提交于 2020-01-23 14:39:49

问题


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.


回答1:


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.




回答2:


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.
}



回答3:


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?




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/2360043/javascript-window-onunload-fires-off-after-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!