By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

need JavaScript to prevent people from closing a browser window

P: n/a
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.

Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <da**********************@freudenberg.de> wrote in
message news:O4**************@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.

Nov 17 '05 #2

P: n/a
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)" <ti*****@despammed.com> schrieb im Newsbeitrag
news:ON**************@TK2MSFTNGP09.phx.gbl...
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" <da**********************@freudenberg.de> wrote in
message news:O4**************@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.

Nov 17 '05 #3

P: n/a
"Daniel Walzenbach" <da**********************@freudenberg.de> wrote in
message news:us**************@TK2MSFTNGP12.phx.gbl...
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?


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
jo***********@surfcontrol.com
Nov 17 '05 #4

P: n/a
Hi Daniel,

Based on my research and experience, we can hook the onbeforeunload event
of the body tag. Here is the sample HTML, please try it.

<HTML>
<HEAD>
<SCRIPT>
function closeIt()
{
event.returnValue = "Any string value here forces a dialog box to \
appear before closing the window.";
}
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</BODY>
</HTML>

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #5

P: n/a
Jacob,

Again you helped me and I am really thankful. Have a wonderful day.

Daniel
"Jacob Yang [MSFT]" <ji***@online.microsoft.com> schrieb im Newsbeitrag
news:EK**************@cpmsftngxa06.phx.gbl...
Hi Daniel,

Based on my research and experience, we can hook the onbeforeunload event
of the body tag. Here is the sample HTML, please try it.

<HTML>
<HEAD>
<SCRIPT>
function closeIt()
{
event.returnValue = "Any string value here forces a dialog box to \
appear before closing the window.";
}
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</BODY>
</HTML>

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #6

P: n/a
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?";
}
Nov 17 '05 #7

P: n/a
"Daniel Walzenbach" <da**********************@freudenberg.de> wrote in
message news:uH**************@TK2MSFTNGP10.phx.gbl...
I want to open a web form without the navigation bar (this is no problem and I know how to do this). Next would be that a user changes data on this page and clicks the (x) at the upper right to close the form. I then want to stop the page from being closed and ask the user if he wants to save his changes. If he chooses to do so then I want to jump back in the code behind and save the data, otherwise the form will be shut.


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
jo***********@surfcontrol.com
Nov 17 '05 #8

P: n/a
Bear in mind that while Jakobs code allows you to correctly prompt the user,
it does not determine if the user has changed anything in the screen, so you
will need to add in the extra step to ensure that you have this covered.
Also, it will always prompt - even if the user has not changed anything -
which may be a wee bit tedious for an end user. So take the example and
expand on it to get the best out of what is being suggested here.

--
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" <da**********************@freudenberg.de> wrote in
message news:Oy**************@tk2msftngp13.phx.gbl...
Jacob,

Again you helped me and I am really thankful. Have a wonderful day.

Daniel
"Jacob Yang [MSFT]" <ji***@online.microsoft.com> schrieb im Newsbeitrag
news:EK**************@cpmsftngxa06.phx.gbl...
Hi Daniel,

Based on my research and experience, we can hook the onbeforeunload event of the body tag. Here is the sample HTML, please try it.

<HTML>
<HEAD>
<SCRIPT>
function closeIt()
{
event.returnValue = "Any string value here forces a dialog box to \
appear before closing the window.";
}
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</BODY>
</HTML>

Does it answer your question? If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.