Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: onUnload problem.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onUnload problem.

    Hello everyone,
    I am trying to use a confirm close script but unfortunately I cannot get it to work.
    Here's my script:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function confirm(){
    var confirm = window.confirm("Are you sure you want to close this window??");
    if(confirm){
    return;
    } else {
    return false;
    }
    }
    window.onunload = confirm;
    </script>
    </head>
    <body>
    
    </body>
    </html>
    It doesn't even show the confirm and also gives an error " too much recursion". Any help would be greatly appreciated.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A lot of browsers kill scripts that block the current thread when unloading, I believe.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    A lot of browsers kill scripts that block the current thread when unloading, I believe.
    What has browsers to do with this thread??

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    He means that (some) browsers won't let you do that. They won't let you prevent someone from leaving a site. No matter whether you gave the user an option or not.

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    The onUnload event handler isn't stable in all browsers so as said above it won't work in some browsers.
    - Mike

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What if I want to call an ajax function when the onunload event gets triggered?? Even that is not working correct.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can call it, but before it completes the page will close and the connection will terminate. Sorry, there's not much you can do about it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi
    It doesn't even show the confirm and also gives an error " too much recursion".
    That's because your code calls itself in an infinite recursive loop.

    The window object is, by convention, a reference to the global object. Function and variable declarations, when occuring outside the bodies of other functions, are added as properties of the global object.

    If you want to prompt users to confirm closing, use the beforeunload event (not universally supported nor standard, but the right event). The listener (the handling function) should return a string containing a reason for the prompt message. This message is added to other built-in text that constitutes the dialogue box; the latter cannot be removed.

    Code:
    this.onbeforeunload = function() {
        return 'Please make sure you have saved any information before proceeding';
    };
    Note that using the phrase "close this window" is inadvisable: the beforeunload event (and the unload event) occur for many reasons not limited to closing a window or tab.

    Mike

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Quote Originally Posted by mwinter
    That's because your code calls itself in an infinite recursive loop.
    Now I feel really stupid for not spotting that.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    What? how is that recursive? I don't understand... Does it have to do with the function returning false?

Bookmarks

Posting Permissions

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