need JavaScript to prevent people from closing a browser window

Discussion in 'ASP .Net' started by Daniel Walzenbach, Oct 13, 2003.

  1. 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.
     
    Daniel Walzenbach, Oct 13, 2003
    #1
    1. Advertising

  2. 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" <> wrote in
    message news:...
    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 Timney \(Microsoft MVP\), Oct 13, 2003
    #2
    1. Advertising

  3. 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)" <> schrieb im Newsbeitrag
    news:...
    > 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" <> wrote in
    > message news:...
    > 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.
    >
    >
    >
     
    Daniel Walzenbach, Oct 14, 2003
    #3
  4. "Daniel Walzenbach" <> wrote in
    message news:...
    > 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
     
    John Saunders, Oct 14, 2003
    #4
  5. 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 Yang [MSFT], Oct 15, 2003
    #5
  6. Jacob,



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



    Daniel


    "Jacob Yang [MSFT]" <> schrieb im Newsbeitrag
    news:...
    > 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.
    >
     
    Daniel Walzenbach, Oct 15, 2003
    #6
  7. 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?";
    }
     
    Hattori Hanzo, Oct 15, 2003
    #7
  8. "Daniel Walzenbach" <> wrote in
    message news:...
    > 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
     
    John Saunders, Oct 16, 2003
    #8
  9. 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" <> wrote in
    message news:...
    > Jacob,
    >
    >
    >
    > Again you helped me and I am really thankful. Have a wonderful day.
    >
    >
    >
    > Daniel
    >
    >
    > "Jacob Yang [MSFT]" <> schrieb im Newsbeitrag
    > news:...
    > > 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.
    > >

    >
    >
     
    John Timney \(Microsoft MVP\), Oct 18, 2003
    #9
  10. Daniel Walzenbach

    sandip25pa

    Joined:
    Jan 25, 2010
    Messages:
    1
    Value set after onbeforeunload not available at server side.

    I am not able to get the form field values set in onbeforeUnload on server side. Can you please help...

    I have modified the closeIt() function as below

    function closeIt()
    {
    var result=confirm("Do you want to save the changes before navigating.");
    if (result==true)
    { document.frmname.hGridStateModified.value ="YES";
    return ;
    }else { document.frmname.hGridStateModified.value ="NO";
    return ;
    }
    }

    <input type="hidden" name="hGridStateModified" value=""/>

    In filter/servlet I am doing..
    String gridState = (String) request.getParameter("hGridStateModified");

    The value I am getting for gridState is null. I am expecting it as 'YES' or 'NO'



    Can you please help ?
     
    sandip25pa, Jan 25, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul
    Replies:
    8
    Views:
    6,709
    data64
    Aug 14, 2004
  2. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    678
    Thomas Hansen
    Feb 15, 2007
  3. Replies:
    1
    Views:
    749
    Roger Lindsjö
    Jun 11, 2008
  4. thomas
    Replies:
    0
    Views:
    251
    thomas
    Oct 23, 2003
  5. danny

    Prevent window closing

    danny, Dec 23, 2003, in forum: Javascript
    Replies:
    11
    Views:
    266
    Grant Wagner
    Jan 2, 2004
Loading...

Share This Page