SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    589
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript confirm close window

    Hello forums. Why is this code always returns true? :

    function closeIt(){
    if(confirm("Are you sure you want to close this window")){
    return true;
    }else{
    return false;
    }
    }

    </script>

    <body onunload="closeIt()">

    I'm trying to make a routine that will confirm the user if he wants to close the pop up window.

    Thanks
    Don't Panic

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    It's not that it always returns true, it's that even when it returns false, it won't stop the window from closing.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The event that he wants to trigger on instead, is the onbeforeunload event.

    Also, instead of

    Code javascript:
    if(confirm("Are you sure you want to close this window")){
        return true;
    }else{
        return false;
    }

    You can simplify that and just return the result from the confirm statement.


    Code javascript:
    return confirm("Are you sure you want to close this window");
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    is the onbeforeunload event.
    Thought that was IE only?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    Thought that was IE only?
    Nope, it's all modern browsers too.

    Sample code:
    Code javascript:
    window.onbeforeunload = function () {
        return "Are you sure that you want to leave this page?";
    }

    Demo page:
    Code html4strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Onbeforeunload test</title>
    </head> 
    <body> 
    <p>This is a test of onbeforeunload. Please click this link to <a href="http://www.google.com">google.com</a></p>
    <script type="text/javascript">
    window.onbeforeunload = function () {
        return "Are you sure that you want to leave this page?";
    }
    </script>
    </body> 
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Nope, it's all modern browsers too.
    Unfortunately - since its main use is to shove more spam in your face when you are trying to leave a spammy web page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by felgall View Post
    Unfortunately - since its main use is to shove more spam in your face when you are trying to leave a spammy web page.
    It depends on how it's approached.

    Google's Gmail for example uses it very wisely. When you are writing an email it says that your message has not been sent if you try to close the window part-way through.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pmw57 View Post
    It depends on how it's approached.

    Google's Gmail for example uses it very wisely. When you are writing an email it says that your message has not been sent if you try to close the window part-way through.
    I agree that there are a few sites where it is useful but its misuse is so widespread that either disabling it completely or automatically confirming without bothering to read it are probably very common and so it doesn't necessarily serve its intended purpose even when used appropriately.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •