473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

need JavaScript to prevent people from closing a browser window

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
8 9184
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
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
"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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Maria Bitsku | last post by:
How do I deactivate a window using Javascript. For example if I have a window that opens up another window, how do I prevent the user from clicking (doing anything) in the original window until...
8
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
11
by: danny | last post by:
Is there a way I can prevent the browser window from being closed? I'd like to make sure the browser window only closes programatically (I want to make sure the user enters data before moving on)....
2
by: dsnyder | last post by:
This HTML has a bit of Javascript at the end that puts the initial focus on the userID field. It works great on Windows2000 running IE6, but the initial focus never goes to the userID field on...
9
by: CW | last post by:
I wrote an HTML based chat application. The front end is built entirely on HTML + javascript. Essentially, I have a hidden frame that's refreshed frequently and any new messages are displayed in...
4
by: Daniel Walzenbach | last post by:
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...
7
by: Jaggu | last post by:
Hi , I need to close main window, once the child window succesfully opens else main window to remain. In my case when I close the main window immediately after the "window.open()" as mentioned...
1
by: karthik juneni | last post by:
Hi all, Iam trying to capture windows closing event (i.e) when the user clicks on the "X" button i want to capture that event and want to update some values in the database.I tried two methods...
3
by: Jimmy | last post by:
It is also possible for popup window to call function in main window by using the opener property. Will "opener.someFunctionInMain(param1, param2)" in the popup window work? It's possible for...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.