How to Detect whether User has click the browser refresh button or Close button

Many Programmers are in wandering how to detect the User has clicked address bar refresh or a Close button in Internet Explorer.I find one way to work out this Problem.What is the solution and in which scenario it will be useful and how to use i will describe in this Article.

Scenario to detect the Browser refresh or close Button of the User Click:

In Some Scenarios where user in the Application do not Log out properly In that case my application should track the User session Log out time entry in database.Even my application have a log out button but the User will not Log out properly he will click close button in the browser and exit the application.So when the User click Close button in Java script the onbeforeUnload Event comes to Picture and add server-side programming to track the Session log out (by giving a event or Web service method or JQuery or Ajax method in script tags).But if you carefully observe there is a one more action in it that is browser refresh(by mouse click) it will also raise the event onbeforeunload(Unfortunately there is no key code like refresh F5 for Mouse Click). But some how the other we should stop this entry in the Database.How to differentiate these two things how it is possible i will describe in this article.

See the below real time Images and Code

In my Master Page Code....

Javascript BeforeUnload

The below image shows the Adress bar refresh(by Mouse Click).


Now the Code to detect the Refresh button or Close button is only by adding this code in Java script.

window.event.clientX >= 1300

Now i click the the refresh button in my Internet Explorer that will be less than 1300 (Window.Clientx) Size.and i click close button then my Window.Clientx size is greater than or equal to 1300. I assumed its a close button.This is the Only best assumption we will get other than Cookies in Javascript.

Now see the Output images how it looks...

Refresh Alert

Now i click close button through a Mouse Click see how the out put

User Leaving

My debugger in the Server side after clicking close....

Debugger close

if the Refresh is F5 we can easily predict by keycharcode.

Now i am successful to make an entry in the database table.
see in the Database table entry ....

Database table

Summary :

Here i made a check that if the window Client x Coordinate is less than 1300 and it raises the onbeforeUnload event than it is a refresh if the window Client x Coordinate is greater than or equal to 1300 than i assumed it a close.Even when you are working with gaming software the Client coordinates are very Useful.


Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama


Author: ketan Italiya15 Nov 2013 Member Level: Gold   Points : 0

This is a nice article,and superb understanding thanks,

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: