0

I want to perform an action on server side before the client refreshes, closes or loads another page in the browser tab. I know that I can use Ajax for that but the problem is how can I know that the user is trying refreshes, closes or loads another page in the browser tab. So I want to know the event that is triggered with JQUERY.

aidonsnous
  • 1,475
  • 4
  • 19
  • 41
  • By their very nature an action is performed on the server when the user refreshes or loads another page. However, given your cryptic problem statement, it sounds like you're looking for AJAX. – Rory McCrossan Feb 02 '16 at 16:51
  • `$(document.body).trigger('load')` that should do it – Riskbreaker Feb 02 '16 at 16:53
  • @Rory McCrossan Which action is that; Because the big problem for me is I want to identify each tab uniquely(Know from which tab the action is come from) – aidonsnous Feb 02 '16 at 16:59
  • Do you mean tabs within your page, or tabs within the browser. You're really not making this easy for someone to help you. Seeing your current HTML and JS as well as a description of what you want to do would really help a lot. – Rory McCrossan Feb 02 '16 at 17:00
  • I edited the question to make it more clear – aidonsnous Feb 02 '16 at 17:02

2 Answers2

0

If I get you correctly, you want to know when a tab/window is effectively closed. Well, AFAIK the only way in Javascript to detect that kind of stuffs are onunload & onbeforeunload events. Unfortunately (or fortunately?), those events are also fired when you leave a site over a link or your browsers back button. So this is the best answer I can give, I don't think you can natively detect a pure close in Javascript. Correct me if I am wrong.

  • Not closed but before closing,refreshing or loading another page – aidonsnous Feb 02 '16 at 17:11
  • -1 Answer from @jAndy literally, exactly copy-pasted from http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser without giving credit. – Laurens Swart Feb 02 '16 at 17:18
0

I don't think there's a way to check if a user pushes the refresh button (F5) or if a user closes a tab, besides the one previously mentioned by @user5873949.

By the way, your question, and also user5873949's answer oddly enough, is an exact duplicate of:

javascript detect browser close tab/close browser

So to be honest, this question should be closed... However, there are other ways to detect a refresh with javascript:

Check if page gets reloaded or refreshed in Javascript

Community
  • 1
  • 1
Laurens Swart
  • 1,234
  • 9
  • 24