need JavaScript to prevent people from closing a browser window

  • Thread starter Thread starter Daniel Walzenbach
  • Start date Start date
D

Daniel Walzenbach

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.
 
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>
----------------------------------------------

message 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.
 
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
 
Daniel Walzenbach said:
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?
 
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.
 
Jacob,



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



Daniel
 
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?";
}
 
Daniel Walzenbach said:
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.
 
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>
 
Back
Top