/    Sign up×
Community /Pin to ProfileBookmark

window.onClose …?

Is there such an event which if returned false, the window should not close anymore? I am aware of window.onUnLoad, but I tried

[code]
window.onunload=function() {return confirm(“Close window?”);}
[/code]

…and it didn’t work. I was wondering if anyone knows how I can achieve that?

And another question, if I fire a request (Ajax) on the onUnLoad event, will it reach the server…..?

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Nov 09.2006 — onbeforeunload is the closest you are going to get

FYI onunload and onbeforeunload fire with closeing the browser, refreshing, clicking a link, posting a form, etc!

Will the request back it. No Guarantee

Eric
Copy linkTweet thisAlerts:
@ZnupiauthorNov 09.2006 — Thanks, but is there a way to prevent the user from closing the browser? The only thing I can do with onbeforeunload is display a confirm and ask the user if he/she actually wants to close it. I want to prevent him/her from closing the window, make a request to the server, and close the window using window.close()... Is there any way to achieve this?
Copy linkTweet thisAlerts:
@A1ien51Nov 09.2006 — Imageine if you could not close a web browser when you went to some site on the internet that was loading your computer full of viruses!

Basically you do not have this ability for security reasons. If you are sending a request to kill a session, that is the servers job to expire it.

Eric
Copy linkTweet thisAlerts:
@ZnupiauthorNov 09.2006 — Yes, I know, but I've seen it done on Google's Page Creator. When you hit the "Back" button it first saves everything and then moves away from the page, without any confirm boxes...
Copy linkTweet thisAlerts:
@W8_4meNov 12.2006 — Damn, I was looking for this as well... ah well, people will just have to stop being trigger happy with my page! I have a form which takes a while to fill in, so if they close it, they lose all entered data (the data is not saved anywhere else).
Copy linkTweet thisAlerts:
@grumpyoldtechsNov 12.2006 — face party operate a system which when you close the window (a chat window) it asks you if you want to close the browser window or not so something of your description can be done
Copy linkTweet thisAlerts:
@W8_4meNov 12.2006 — The question is how!
Copy linkTweet thisAlerts:
@grumpyoldtechsNov 12.2006 — i think thats it from the looks of it cba to read it and check

<i>
</i>&lt;script language="JavaScript"&gt;&lt;!--
var legalExit=0;var lpx=10; function leave(){if(legalExit==0){var dl=confirm('WARNING: Closing this window means you cannot receivenPrivate Chat Requests from other members.nnClick OK to remain connected to the Private Chat Server.'); if(dl){if(window.screen){lpx=screen.width;lpx=lpx-225;} var op=window.open('live_connection_ns.aspx?ri=2&amp;ac=','BW2','width=170,height=185,location=no,status=no,directories=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no,top=10,left=' + lpx);}}} function openWin(u,t,x,y,s) { var op=window.open(u,t,'width='+x+',height='+y+',location=no,status=yes,toolbar=no,directories=no,menubar=no,scrollbars='+s+',resizable=no'); op.focus();} // --&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@coppocksNov 12.2006 — FYI ... the window that you want to close should have been opened by javascript. That being said... try putting the onUnload event in the body. Try this:

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;Script Language=JavaScript&gt;
&lt;!-- Hide
function rusure() {
if (confirm("Are you sure you want to close Me?"))
{
window.close();
return true;
}
else {

<i> </i> return false; }

}
// end hide --&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body onUnload="rusure();"&gt;
&lt;form&gt;&lt;input type="button" value="close" onclick="window.close();"&gt;&lt;/form&gt;&lt;p&gt;

&lt;a href="JavaScript:rusure();"&gt;Close Me&lt;/a&gt;
Copy linkTweet thisAlerts:
@ZnupiauthorNov 22.2006 — coppocks, that will not work. The only thing that can be done (so far) is this:
<i>
</i>&lt;body onBeforeUnload="return 'You will lose any unsaved data!'"&gt;

...that will display a confirm box with your message + your browser's message and ask you if you want to close the window or not... I guess just forcing the user not to close the window is impossible... ?
Copy linkTweet thisAlerts:
@silwilhithMay 29.2007 — Damn, I was looking for this as well... ah well, people will just have to stop being trigger happy with my page! I have a form which takes a while to fill in, so if they close it, they lose all entered data (the data is not saved anywhere else).[/QUOTE]
Why don't you use Ajax programming to store user data in user cookies using onChange event of the fields ?

(and inform the user about the purpose of this cookie if he has disabled cookies and/or javascript)
Copy linkTweet thisAlerts:
@felgallMay 29.2007 — If you are using cookies you can access them directly from JavaScript without needing Ajax. The alternative to cookies would be to use Ajax to store the info on the server as each field is entered rather than waiting for the whole form to be submitted.
Copy linkTweet thisAlerts:
@tuseroniJun 28.2010 — alternatively you can make an ajax request onUnload to save the information to the server, or just use local session store from the new html5 standard (window.localStorage)

if you have a lot to store you can save some headache by taking the whole form into an array and using JSON.stringify on the array then storing that string in local storage, then all you have to do is use JSON.parse on the string when you get it out of local storage.

local storage persists over many windows, so it will be there when you come back, no need to store anything serverside.
×

Success!

Help @Znupi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...