- need JavaScript to prevent people from closing a browser window
- Posted by Daniel Walzenbach on October 13th, 2003
Hi,
does anybody know the JavaScript the guys at Microsoft used in MS CRM to prevent people from closing a browser window and asking them (on the client) what they really want to do? If you don't know what I'm talking about take a look at MS CRM. Open a form, make some changes and try to close the form without saving first. Instead of closing the form you will be asked if you'd like to save first or discard the changes. I have often asked myself how to design this behavior.
Thank you in advance.
Best regards
Daniel Walzenbach
P.S. If you need to contact me simply remove ".NOSPAM" from my email address.
- Posted by John Timney \(Microsoft MVP\) on October 13th, 2003
I dont have code handy - but this is how it works.
You initially load the page with a hidden field of a value 0, and Onchange
event in a client editable item (like a textbox) would set the value to be 1
(indicating a change). On submit - run a function that checks the value of
1 or zero and issues a prompt.
--
Regards
John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------
"Daniel Walzenbach" <daniel.walzenbach.NOSPAM@freudenberg.de> wrote in
message news:O4twcWckDHA.2200@TK2MSFTNGP12.phx.gbl...
Hi,
does anybody know the JavaScript the guys at Microsoft used in MS CRM to
prevent people from closing a browser window and asking them (on the client)
what they really want to do? If you don't know what I'm talking about take a
look at MS CRM. Open a form, make some changes and try to close the form
without saving first. Instead of closing the form you will be asked if you'd
like to save first or discard the changes. I have often asked myself how to
design this behavior.
Thank you in advance.
Best regards
Daniel Walzenbach
P.S. If you need to contact me simply remove ".NOSPAM" from my email
address.
- Posted by Daniel Walzenbach on October 14th, 2003
John,
thank you for your quick answer. I know that it must be done like this
(theoretically), but I don't know exactly what to do to prevent a user from
closing a web form?
I'd be thankful if you or anybody else who knows could provide me with a
more detailed approach of how to solve this problem.
Thank you.
Best regards
Daniel
"John Timney (Microsoft MVP)" <timneyj@despammed.com> schrieb im Newsbeitrag
news:ONb52odkDHA.2652@TK2MSFTNGP09.phx.gbl...
- Posted by John Saunders on October 14th, 2003
"Daniel Walzenbach" <daniel.walzenbach.NOSPAM@freudenberg.de> wrote in
message news:usVPXrpkDHA.2216@TK2MSFTNGP12.phx.gbl...
You want to be a little careful here. You might be able to find a way to
keep a user from inadvertently closing a web form, but you're not going to
be able to shut off all the ways a user could do himself damage. The user
can always turn off his computer, for instance. Less drastically, are you
preventing the user from using the "back" button or keystroke, or
JavaScript?
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com
- Posted by Hattori Hanzo on October 15th, 2003
They used the onbeforeunload event as such:
function window.onload()
{
window.attachEvent("onbeforeunload", OnClose);
}
function OnClose()
{
event.returnValue = "Are you sure you want to close this window?";
}
- Posted by John Saunders on October 16th, 2003
"Daniel Walzenbach" <daniel.walzenbach.NOSPAM@freudenberg.de> wrote in
message news:uHrM1uqkDHA.2424@TK2MSFTNGP10.phx.gbl...
Daniel,
After the user enters some data on the page, and before the user submits the
form, has any data been sent to the database? If not, you might want to
consider the fact that the user probably intended for the window to close
when he clicked the "x". He probably had no reason to believe his data would
be saved. So when you prompt him, he's probably going to click the "shut the
damned window like I told you to!" button. I know I would.
--
John Saunders
Internet Engineer
john.saunders@surfcontrol.com