Detect refresh button in the browser

is there possible that i can detect the user click the refresh button in the browser???:bawling:

You can’t detect the button being pressed, but you can catch the page being unloaded (i.e. when the user navigates to another page or refeshes). Would that help?

<BODY onbeforeunload=“CatchUnload();”>


thanks for replying :slight_smile:

but this one is not the one i want, now the situation is when the user click refresh or leave my website and go to other web site or close window that time i want to update some data in database so how i detect this few thing???

If you are trying to catch when people leave your site, then use onUnload.

onUnload and onBeforeUnload are virtually identical, but subtly different (look them up). In this case, either will do, so fine. However Nakatakyo, the point is that what you are asking for cannot be done, so these suggestions are the closest we can come to providing a solution.

If you explain what you are trying to do a little more I might be able to think of a different approach to your problem.


Ah, just re-read your last post. You want to trigger a server-side action when the client changes/refreshes the URL?

Then use the onBeforeUnload / onUnload event to call a function which uses JSRS ( to call the desired script on the server. In this script you can then so whatever you want.

An alternative would be to use the server-side session end event (ASP has it, so presumable PHP has it too) to detect when the visitor’s session has expired and then execute the tasks that you want.


Just to clarify though, the pressing of the refresh button cannot be detected per se. It could be simulated by the use of JavaScript in another frame, which would be triggered by the page change and compare the new URL to the old URL. If they match, then it was a refresh, and appropriate action can be taken.

However my two previous suggestions (particularly the latter) would be far easier to implement.


thanks for M@rco and weirdbeardmt for replying me

i think i need to tell u all more cleary about my problem

in my website i dont want two same user login at the same time so i save some info inside database to detect it and i always want to know whether the user have log out or the user logout got few way
one is prober logout
two is close the window
three is move to other side

one is no problem to me and two and three also no problem (i use onbeforeunload to detect close window and move to other side) but one problem is when i refresh the page that time the onbeforeunload also detect the action and update my database set the user log out so i need to find the solution to detect the refresh

so got any sugggestion or solution to solve this problem?

Is your site written in ASP or PHP (or something else)?

I have a cunning plan…! (c)Baldrick


i’m using php to develop my site

Here’s the plan:

  • At the top of every script, set the session timeout to 30mins or whatever you want it to be normally.

  • Then, use the onunload or onbeforeunload events (in conjuction with the JSRS library that I gave the URL for earlier) to call a script which sets the session timeout to one minute or so.

  • You then need to catch the session expiry event (however you do it in PHP) and call your log out code when it occurs.

Thus, if the user is leaving the site (by closing the window, or navigating to another site), then his session expires one minute later. If he is only refreshing the page, then as soon as the page is called, the session timeout is reset to its normal value, and he stays logged in!



how i detect the session is expiry after the user leave the site or close the window

and actually i just can catch a little bit wat u say but i think after i know how to catch the session then is ok already

however thanks a lot for helping

In ASP, it’s an event handler that you place in theGLOBAL.ASA file. It should be something similar in PHP, but I don’t program in PHP. I had a quick search, and this should tell you everything you need to know:


Was running late earlier. Just got back and had a better look at that link - PHP does not seem to have an equivalent to ASP’s Session_OnEnd() event…

Any PHP coders know any different?


I’m not entirely sure how you would use it, but it would be either session unregister or [URL=]session destroy. I suppose technically (depending on what you are doing with it) you could use [URL=]session write close. :slight_smile:


Isn’t the onbeforeunload handler only handler with IE. Last time I checked it was an IE specific handler (as most of the good ones are).

so actually php support the method or not?

maybe i also go find some information and tell u, ok

anyways thanks all of u have answer my question

Originally posted by nakatakyo
[B]so actually php support the method or not?

maybe i also go find some information and tell u, ok

anyways thanks all of u have answer my question [/B]

Sorry to confuse you but php doesn’t support any live methods. It is done once the page is loaded. Javascript is the ‘onbeforeunload’ :slight_smile:

RE: onBeforeUnload
I think you may be right about IE-only onBeforeUnload - use onUnload instead.

RE: PHP Session End
ASP code is executed linearly, but it does support four events which you can use to execute code when they are triggered. They are:

Application Start
Application End
Session Start
Session End

It would appear that PHP does not feature these vital events. This was a difference between the two language which I was unaware of - I had previously thought that both were equivalent in functionality in that there is nothing that can be done in one that cannot be done in the other. I now see that this is incorrect, the case in hand being one example. Are there any other “missing features” in PHP compared to ASP?


so that mean php cannot support this method then mabe i need to think other way to handle the detect user logout or leave the site but however thanks to all have answer and help me