Link to home
Start Free TrialLog in
Avatar of jgenyuk
jgenyuk

asked on

How to make onUnload not execute on reload

I need to execute an action only when the user closes window, but not when he hits Reload. onUnload is fired in both cases. Any trick to do this?
Avatar of third
third
Flag of Philippines image

no, you cannot determine that a user pressed refresh/reload or closed the window.
Avatar of bgbuck7
bgbuck7

There is no way that I could think of.

reload is a browser function that is not affected by JS, and you can't use frames for this, because the frames will be reloaded as well. If you want the user to be able to reload the page without the function to fire you could do something like:

<script>
var abc = 0
function callFunc(){
if (abc == 0)
alert("Unloaded!")
else
alert("Reloaded!")
}
</script>
<body onUnload="callFunc()">
<A HREF="#" onClick="abc=1;location.reload();return false;">Click to Reload</A>

If you click the link, the page is reloaded and it says "reloaded!"
But if you push f5 or click your browser's reload button, it will alert "unloaded!"
and of course if you navigate away or close the window it says "unloaded!"
Because you will not know which page your browser is navigating to, you can't differentiate if the page navigates to the same page or a different one.
onUnload will always fire when the page is unloaded - even when the page is being refreshed/reloaded. There's not much you can do about it.

Perhaps you may want to defer your onLoad code to the next page...?

skip
you can make the user not to use this button by showing contents of your page in a newly open window without toolbar -

- use a "splash screen" to welcome the user

- add onclick / "on setTimeout" function that opens new window (window.open (...))

- set "toolbar=no" as one of the options in the third parameter of window.open
how about F5 or CTRL+N on the popup window?
Yes, szczepan, that'll get rid of the Refresh/Reload button, but can't the user right-click on the window and select "Refresh"/"Reload" from the popup menu?

skip?
<BODY oncontextmenu="return false;">
....
</BODY>
Maybe you can just provide users your own link for reloading (like on ee) which they will be using for reloading.
Avatar of jgenyuk

ASKER

I feel I need to clarify the situation.
The user is doing some online content editing. I'm using PHP scripting for this. He can save his work by pushing the "Save" button. When changes are made, the page is reloaded automatically to reflect them.

I want to avoid the situation when the user closes the window without saving his work. I'd like to prompt him to save if this happens. I don't really care if he hits Reload, but automatic reloading is a problem.

Sorry for not making it clear before.
ASKER CERTIFIED SOLUTION
Avatar of bgbuck7
bgbuck7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(IE only)
Avatar of jgenyuk

ASKER

Nice and easy solution from bgbuck7.
But you cannot really cancel window closing, or can you?
Yes, it works for window closing as well!
Avatar of jgenyuk

ASKER

I mean, if onBeforeUnload returns false, the window still will close.