How can I detect a user has clicked the X to close the Browser Window

T

tarun.kataria

Hi All,

Is there any way to detect that the user is trying to Xout the browser
window instead of hitting a button. Because in my application I want to
make it sure that they dont do it either knowingly or accidently.
Its an ASP.NET application with C#.

Thanks
Tarun
 
T

tarun.kataria

Thanks for your reply. But I tried it and it doesnt work. actually
there is no onclose event in javascript for window object. I also found
this discussion on a forum

http://www.thescripts.com/forum/thread431659.html

but they also dont have a concrete answer to how to implement it.

if anybody encountered the same problem please let me know.

thanks
 
G

Guest

I don't think you can stop someone closing their browser window, you can only
do something such as pop up a new window if they do. Think how iritating it
would be if websites could stop you closing the browser.
 
K

Kevin Spencer

The truth is, there is no way to do this. The Mozilla object model supports
it, but Internet Explorer does not. It only supports "onbeforeunload" which
fires any time the *document* is unloading from the window, including, but
not limited to, when the window is closed. There is no existing standard for
it, so as to whether there ever *will* be an "onclose" event in all
browsers, your guess is as good as mine, and I'm not guessing.

A browser will never behave the same way as a desktop application. You can't
prevent people from either accidentally or intentionally closing their
browser. In any application, it is impossible to prevent people from doing
absolutely anything wrong, and closing an application isn't wrong. At a
certain point, you have to expect the user to assume a certain amount of
responsibility for what they do. And that's a good thing.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
T

tarun.kataria

Thanks for so many responses. I am completely agree with Mr.Spencer
that user should take some responsibilities that how to use the
application. But ask me, I work in a mortagage company and the loan
agents cant even follow that if some field is required (I show the
error message near the control that its required by using validation
controls) then they wont be able to submit it. They come to me and saqy
"its not working".After so many instructions and training sessions I
put the validation summary which pops open in front of there eyes and
now they can understand it. I think we should design our applications
by assuming that its gonna be used by monkeys and you cant control
their behaviour.
As far as the solution to the problem is concerned what Patrick has
told me it works but now the problem is that I have some controls on
that form for which AutoPostBack property is true. So even if the user
selects a radio button from radio button list, the page post backs, and
before that it gives you the same confirmation box. I only want that
confirmation box when the user is clicking the X button.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top