Overriding onBeforeUnload dialog message
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Overriding onBeforeUnload dialog message

  1. #1
    Join Date
    Feb 2006
    Posts
    12

    Overriding onBeforeUnload dialog message

    Hi there,

    Does anyone know how to suppress the default messages?

    1. "Are you sure you want to navigate away from this page?"
    2. "Press OK to continue, or Cancel to stay on current page"

    I need to provide the user a custome message. I tried many approachs, but non of them seem to work. I tried to use a confirm box instead, but for some reason, the return value for onBeforeUnload event seem to be used as string, so return false to the event CANNOT stop the page from refreshing.

    Thanks,
    Welles

  2. #2
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: Overriding onBeforeUnload dialog message

    What code are you using? I'm not sure on how to solve the problem, but I'd like to see what you are trying ...
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Overriding onBeforeUnload dialog message

    Well, onbeforeunload is only JavaScript and VBScript. Which one are you using? What are you trying to accomplish? And why can't you not use onbeforeunload?

    As Dr. Script pointed at, we need to see some code. Can you please post it?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Feb 2006
    Posts
    12

    Re: Overriding onBeforeUnload dialog message

    Thanks for you guys' reply, one of my many attemp to solve this problem is to define a new function for the event but it still doesn't work.

    my goal: to define a custom dialog box (that is, no other message would appear on the dialog box except the message i pass to it)



    Code:
    <script language="javascript">
      window.onbeforeunload = onBeforeUnload_Handler;
      
      function onBeforeUnload_Handler(){
       fConfirm('param1','param2','param3','param3');
      }
    
    
    
    function fConfirm(p1,p2,p3,p4)
    {
    
       //do something with p1, p2, p3, p4....
    
       
    
       //this shows the default dialog box  
       //event.returnValue = 'Any changes will be lost if you proceed.';
      
    
      //this shows the custom confirm box, but it doesn't stop the page from 
    
      //refreshing even the user click the "Cancel" button
      if(!confirm("Any changes will be lost if you proceed.")){
          return false;
      }
      return true;
     }
    }
      </script>

  5. #5
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Overriding onBeforeUnload dialog message

    So you want a confirm box that will display a message you tell it to and do what? Is this supposed to run when someone tries to leave/close your page?

    Remember that onbeforeunload is IE only. You can use onunload for multiple browser compatibility.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Feb 2006
    Posts
    12

    Re: Overriding onBeforeUnload dialog message

    I want the confirm box to pop up when the user leaves a page with unsaved data.

    Actually, that was what I thought before. But I tested onBeforeUnload on FixFox and it works. O, well, since most of my users use IE, so this is not a big problem for me.

  7. #7
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Overriding onBeforeUnload dialog message

    Quote Originally Posted by welles
    Actually, that was what I thought before. But I tested onBeforeUnload on FixFox and it works.
    That is because Firefox is an open-source multibrowser compatible internet browser. I know it is rare but Netscape (unfortunately) still exists.

    And I assume you want the leave to be cancelled if he/she clicks the "Cancel" button on the confirm. This would show that he/she wants to save the unsaved data.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    Feb 2006
    Posts
    12

    Re: Overriding onBeforeUnload dialog message

    Exactly, but for some reason, the "Cancel" button doesn't stop the page from refreshing.

    for example:
    Code:
    <!--This wouldn't stop the page from refreshing wether the user clikcs 'OK' or 'Cancel'-->
    <page onBeforeUnload="confrim('Are you sure to leave without saving data?')">
    
    <!--This would put "True" on the default onBeforeUnload dialog box if the user clicks "OK", and "False" if the user click "Cancel". Note, different from other event, the return value is seen as a string instead of a boolean.-->
    <page onBeforeUnload="return confrim('Are you sure to leave without saving data?')">

  9. #9
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Overriding onBeforeUnload dialog message

    How about something more along the lines of returning false and calling a function?

    Code:
    <script language="JavaScript">
      function checksave(){
        if(confirm("Are you sure to leave without saving data?")){
          self.close();
        }
      }
    </script>
    <body onunload="return false; checksave()">
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    Feb 2006
    Posts
    12

    Re: Overriding onBeforeUnload dialog message

    I've already tried to use unload, but it seems like that it is too late to do the checking in the unload event. The page would be refreshed whether the user chooses OK or Cancel.

  11. #11
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Overriding onBeforeUnload dialog message

    Quote Originally Posted by welles
    I've already tried to use unload, but it seems like that it is too late to do the checking in the unload event. The page would be refreshed whether the user chooses OK or Cancel.
    Then use onBeforeUnload. The following works perfectly for me.

    Code:
    <html>
    <script language="JavaScript">
      function checksave(){
        if(confirm("Are you sure to leave without saving data?")){
          self.close();
        }
      }
    </script>
    <body onBeforeUnload="return false; checksave()">
    </body>
    </html>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  12. #12
    Join Date
    Feb 2006
    Posts
    12

    Re: Overriding onBeforeUnload dialog message

    Thanks for your help. But are you sure you code works?

    When I run you code, I only see the the default onBeforeUnload dialog box with the work "false" in the middle and never see your custom confirm box.

    Like I said before, the onBeforeUnload event read the return value as a string and print it on the default onBeforeUnload dialog box.

  13. #13
    Join Date
    Feb 2006
    Posts
    12

    Re: Overriding onBeforeUnload dialog message

    On a second thought, you code wouldn't work event the event read the return value as boolean. Because it returns before it call checkSave(), so checkSave() would never be called.

  14. #14
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Overriding onBeforeUnload dialog message

    Quote Originally Posted by welles
    But are you sure you code works?
    Have you even tried it?

    I did say...
    The following works perfectly for me.
    Quote Originally Posted by welles
    On a second thought, you code wouldn't work event the event read the return value as boolean. Because it returns before it call checkSave(), so checkSave() would never be called.
    Yes, it does. The return is only on the event handle itself not the rest of the onBeforeUnload commands.

    I have done this code in IE, Firefox, and Safari. It works.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  15. #15
    Join Date
    Sep 2007
    Posts
    1

    Re: Overriding onBeforeUnload dialog message

    Hi Welles,

    I also want to achieve a similar behavior as you. I need to capture the user clicking on the "cancel" button in the default prompt. But am not able to do it. So, instead i tried to use my own custom message using the "confirm" function but am not able to pass this result to the onbeforeunload function without calling the default prompt again. Did you find any solution for your problem. If so, please share you solution.

    Regards,
    rsarun

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center