Javascript window.onunload 在 Page_Load 之后触发

发布于 2024-08-23 08:54:11 字数 296 浏览 3 评论 0原文

我注意到 window.onunload 事件在 page_load 事件之后触发,这是没有意义的。

这种行为给我带来了一个问题 - 在我的卸载中,我清除了会话,因此如果在卸载之前先进行 Page_Load,则显示的页面上会出现错误。

我希望 javascript onunload 在 Page_Load 之前触发......这是正确的假设吗?

澄清一下: 假设我在 test.aspx 页面上,然后单击转到同一页面的链接(假设我单击菜单),我观察到 Page_Load 首先触发,然后 onunload 触发。 完全没有意义。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

少女净妖师 2024-08-30 08:54:11

这是特定于浏览器的行为。 Chrome 和 FF 将在 onunload 触发之前发送 GET 请求,IE8 将首先执行 onunload。不确定其他浏览器如何处理它。最好不要依赖此功能。

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.

浮光之海 2024-08-30 08:54:11

您是否考虑过为您的页面使用通用基类,并在请求不是回发时清除其中的会话(我假设您正在使用会话进行回发)?

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();
    }          
}

那么需要清除会话的页面可以声明为:

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

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.
}
所谓喜欢 2024-08-30 08:54:11

我猜想,只有当您必须渲染导航到的新页面时,window.unload 实际上才会触发(也就是旧的 DOM 被拆除以代替一些新的 HTML)。在服务器返回带有要显示的 HTML 的响应之前,浏览器不知道要呈现什么内容。在页面生命周期(包括 Page_Load)完成之前,不会生成 HTML。因此 page_load 在 window.unload 之前?

无论如何,如果您可以在 window.unload 期间清除会话,为什么不直接清除它以响应某些用户交互并对此更明确一点呢?

编辑:您也可以尝试window.onbeforeunload吗?

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?

逆蝶 2024-08-30 08:54:11

onunload 事件确实会在向服务器发出新页面请求之前触发,因此它肯定会在 Page_Load 方法在服务器上运行之前触发。

问题很可能是您从 onunload 事件向服务器发送另一个请求。由于 IIS 一次只处理每个用户的一个请求,因此该请求将排队并在新页面请求之后执行。

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.

旧话新听 2024-08-30 08:54:11

您可以编写一个实用程序函数来处理会话变量的删除,然后在相应的菜单单击事件中调用该函数。这应该更容易使用,因为窗口卸载仅在页面加载后触发。

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.

贱贱哒 2024-08-30 08:54:11

听起来您正在使用会话来保存逐页更改的临时变量。我想说Session不太适合这种场景。更好的解决方案是使用 Httpcontext 的 Item 集合,该集合的范围仅基于每个请求。它在存储数据时的工作方式与 Session 相同。

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

由于它的范围仅限于每个请求,因此无需使用 JavaScript 来清除每个页面请求中存储的项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文