Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Internet Explorer closing event -catching in JavaScript

    Hello All of You,
    I want to know that
    Is there a way of catching a form_unload a type of event for Internet Explorer, with a cancel= true type of parameter?
    I mean when a form/IE is exiting without saving, it gives you a chance to ask the user to confirm " Exit without saving?"

    Thanxx......

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
    document.body.onunload = function(){event.returnValue=confirm("Really quit?");}
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • Users who have thanked mjlorbet for this post:

    mithilesh (04-25-2008)

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thnxxx a Lot mjlorbet

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by mjlorbet View Post
    Code:
     
    document.body.onunload = function(){event.returnValue=confirm("Really quit?");}
    The snag it that regardless of the response the page still unloads. The onunload event cannot be cancelled as it has already been initiated.

    Try this:-

    Code:
    <form name = "myForm">
    <input type = "text" name = "field1" size = "20">
    <input type = "submit" value = "Submit" onclick ="setFlag()">
    </form>
    
    <script type = "text/javascript">
    
    window.onbeforeunload = confirmExit;
    var postback = false;
    
    function setFlag() {
    postback = true;
    }
    
    function confirmExit() {
    if (document.myForm.field1.value.length > 0) {
    if (postback == true) {
    event.cancelBubble = true;
    }
    else {return "Please don't leave this page without submittting the form.  If you do leave all your data will be lost.";
    }
    }
    }
    
    </script>
    Where quality is the thing sought after, the thing of supreme quality is cheap, whatever the price one has to pay for it. --William James
    Last edited by Philip M; 04-25-2008 at 01:05 PM.

  • #5
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    good call
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

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