Is there any way to know that user leaving a page with asp.net?

后端 未结 5 1943
遥遥无期
遥遥无期 2020-12-10 08:54

My question is a little bit tricky. at least I think. Maybe not, anyway. I want to know if there\'s any way to know if the user is leaving the page. Whatever if he clicks \"

5条回答
  •  长情又很酷
    2020-12-10 09:39

    You can use javascript to tell the server when the user leaves the page. But the webserver washes it's hands of the page once it leaves the server, while the user might keep the page open for a week.

    If you use javascript on the page to fire off a notice to your server when the page unloads you can take some action. But, you can't tell if he's leaving your page for another one of your pages, another website, or closing the browser.

    And that last notice isn't guaranteed to always be sent, so you can't rely on it completely.

    So using the javascript notice to clean up objects (caches or sessions) is a flawed system. You're better with cache & session invalidation strategies that are independent of the onunload notice.

提交回复
热议问题