Very simple popup/confirmation window

Discussion in 'JavaScript' started by REY619, Apr 4, 2011.

  1. #1
    Hello guys, I need a very simple popup/confirmation window that shows when someone closes the tab or browser, or try to navigate away from the site. I have this code but there is some error in it, it wont work properly.. Can someone help?

    <script type="text/javascript">
    function ExitNotice()
    {
           var msg = confirm("You are now leaving the web site.\n"
                + "To leave our site for the link you selected, click OK.\n"
                + "Or Click Cancel to remain.\n\n"
                + "Thank you for visiting our site.");
           if (msg==true)
    { return true;}
    else
    {return false;}
    }
    </script>
    
    Code (markup):
    and edited the body tag like this -
    <body onUnload="ExitNotice()">
    Code (markup):
    what I want is, if they click OK the page closes normally, if they click cancel they stay on the current page.. Currently no matter they click OK or Cancel the page closes.. :\

    I am missing out on something minor..

    Thanks.
     
    REY619, Apr 4, 2011 IP
  2. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #2
    The message is displayed in IE9 and Chrome but not in FF4. In FF3 is the message probably displayed.
    Opera 11 doesn't know onbeforeunload event.

    for FF4 check these pages:
    https://developer.mozilla.org/en/DOM/window.onbeforeunload
    https://bugzilla.mozilla.org/show_bug.cgi?id=588292

    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <title></title>
      <script type="text/javascript">
          window.onbeforeunload= function(){
            return 'You are about to leave this page, OK?';
        };
      </script>
      </head>
      <body>
      aaa
    
      </body>
    </html>
    
    Code (markup):
     
    Jan Novak, Apr 4, 2011 IP
  3. REY619

    REY619 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks onbeforeunload seems to work so far as I wish, but is there a way so that it doesn't display the pop-up/confirmation when the links within the same site are clicked?

    Thanks.
     
    Last edited: Apr 4, 2011
    REY619, Apr 4, 2011 IP
  4. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I don't understand. Could you show the code, please?
     
    Jan Novak, Apr 4, 2011 IP
  5. REY619

    REY619 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I mean the code you provided is working fine, it shows the popup when someone tries to close the page or tries to click a link. But it also appears when someone clicks the subpages of the same site.

    Can something be done so that it only pops up when an external link is clicked, and not the link of the same site.

    I hope you understand..

    Thanks.
     
    REY619, Apr 4, 2011 IP
  6. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #6
    It is not possible, because you can catch where user go. And this is sensitive information. However this code works in FF4, but not in IE9. I have no time to try another browsers, because I think something like this is against users.

    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <title></title>
      <script type="text/javascript">
          window.onbeforeunload= function(e){
    //e is not supported in IE9 here
            console.log(e.target.activeElement.attributes[0].nodeValue)
            console.log(e.target.links[0].attributes[0].nodeValue)
            if(e.target.activeElement.attributes[0].nodeValue.indexOf("http")!=-1){
              return 'You are about to leave this page, OK?';
            }
        };
      </script>
      </head>
      <body>
    
      <a href="b.html">bbb</a>
      <a href="http://google.com">bbb</a>
    
      </body>
    </html>
    
    Code (markup):
    But there is a workaround. Instead of hyperlinks use AJAX to load new content to your page and have only one page :).
     
    Jan Novak, Apr 4, 2011 IP