-1

I want to put some logic before user leaves a page, but I want to handle reload button & close button action differently. All I know is that there is onbeforeunload event that will be called before user unload the page, but it doesn't solve my use case.

I want to create some statistic/tracker, something like this:

$(window).beforeunload(function() {
    if (user click browser's close button)
        // track_user_click_close_button()
    else if (user click browser's reload button)
        // track_user_click_reload_button()

});

2 Answers2

1

You can't know the URL user will be navigating to. This is not a jquery/js issue. The bottomline is - you cannot do what you want.

Update

But if you try sometimes, you might find - you get what you need.

As stated in this SO answer to a similar question, there is something you can do to overcome this inability. I strongly suggest you to read the described approach.

If it's not suiting your needs, please open a new SO question with detailed explanation of what you're trying to do, and we'll try to help you.

Another update

Unfortunately, there's no way of determining this out-of-the-box. You can try the following approach:

$(window).beforeunload(function() {
    // should be a synchronous AJAX-call, as async one won't work
    persistLeaveOnServer();
});

$.ready(function() {
    tellServerWeAreBack();
});

And on server side you should maintain the session state according to that. Once certain amount of time has passed (probably, several seconds), you consider the session ended and persist the statistics as UserClickClose. If tellServerWeAreBack request is received before this time passed, you save UserClickRefresh stat value.

Community
  • 1
  • 1
Dethariel
  • 3,394
  • 4
  • 33
  • 47
  • Hi Dethariel. I don't want to know the URL user will be navigating to. I just want to add some logic* when user click browser's refresh and close button. *) something like tracker: how many user click 'reload', how many user click 'close'. – Andri Setiawan Oct 24 '14 at 08:11
  • Is it some javascript logic? What exactly are you trying to accomplish? The question as it is now is something that's hard to help on – Dethariel Oct 24 '14 at 08:12
  • Thanks for the pointer (http://stackoverflow.com/questions/568977/identifying-between-refresh-and-close-browser-actions/13916847#13916847). I tried to search before but couldn't find it. – Andri Setiawan Oct 24 '14 at 08:27
0

beforeunload fires whenever the user leaves the page. If you want to differentiante between a refresh and a close you must be able to detect the page reload.

You could keep an information telling the page that it is unloading at xx:xx time. Log the fact to some place. When the page is loaded, check whether there is some logged evidence that the page was quit recently (depending on your definition of recently) and turn the event into a refresh event: (see here for an example)

This may not be able to differentiate between a refresh and a user closing then reopening the page quickly (under your time treshold).

Note: the linked page mentions using hidden form elements that are persisted between refresh, but says that browser support is all over the place; I'm wondering if this is still the case or not. Storing information in the window object will survive refreshes so you may want to explore this avenue

samy
  • 14,832
  • 2
  • 54
  • 82