1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

window.onbeforeunload

Discussion in 'JavaScript' started by sojic, Aug 24, 2010.

  1. #1
    I need to "redirect" after clicking CANCEL onbeforeunload.

    So... On Before Unload, confirm appeared. If I click OK, the window will close, if I click CANCEL I'm staying on current page.

    So... I need after clicking CANCEL to redirect to another page.

    
        window.onbeforeunload = function(e) {
            e.returnValue="My custom message";
            window.location="http://www.google.com";
        };
    
    Code (markup):
    The problem with this code is that "window.location" execute on before unload... when confirm appears. I need it to be executed after CANCEL clicked.

    Any solutions?
     
    sojic, Aug 24, 2010 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You could use a timer:
    
        window.onbeforeunload = function(e) {
            e.returnValue="My custom message";
            window.setTimeout(function() {
                  window.location="http://www.google.com";
            }, 2000);
        };
    
    Code (markup):
     
    camjohnson95, Aug 24, 2010 IP
  3. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Currently, this code "don't work properly"... If I click cancel, it ask me again, and again... if I click OK... it redirects me to google.
     
    sojic, Aug 24, 2010 IP
  4. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #4
    sojic, Aug 24, 2010 IP
  5. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Is there another event... that detects window close... not "native" url changing. Native url changing, I mean if somebody click on the link on the page... it is also onbeforeunload.
     
    sojic, Aug 24, 2010 IP
  6. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #6
    With timer does not work because of infinite loop... when redirecting... it is another "onbeforeunload"... if there is any other method to detect "window close", not window.unload... it will be better.
     
    sojic, Aug 24, 2010 IP
  7. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #7
    Hey I'm curious too.
    How about this:
    <script type="text/javascript">
    //<![CDATA[
    var
      is_asked = false;
    
    window.onbeforeunload = 
      function (ev) {
        var e = ev || window.event;
        window.focus();
        if (!is_asked){
          is_asked = true;
          var showstr = "CUSTOM_MESSAGE";
          if (e) {  //for ie and firefox
            e.returnValue = showstr; 
          }
          return showstr; //for safari and chrome
        }
      };
    
    window.onfocus =
      function (ev){
        if (is_asked){
          window.location.href = "http://www.google.com";
        }
      }
    
    //]]>
    </script>
    
    Code (markup):
     
    hdewantara, Aug 25, 2010 IP
  8. sojic

    sojic Active Member

    Messages:
    133
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Great.... it works great.


    Thanks!!!!
     
    sojic, Aug 25, 2010 IP
  9. matrixxx68

    matrixxx68 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    really great work! I searched a day long to find this script for safari (firefox no problem).
    But can I stop this script when the user click on a link?

    For IE and Firefox I used this one:

    <head>
    <script type="text/javascript">
    var exit=true;
        function confirmExit()
                {
                if(exit)
                location.assign('chance.php');
                if(exit)
                return "Message Line 1\n\nMessage Line 2\n\nMessage Line 3\n\nMessage Line 4\n\nMessage Line 5\n\nMessage Line 6\n\nMessage Line 7\n\nMessage Line 8\n\nMessage Line 10\n\nMessage Line 11\n\nMessage Line 12";
                }
    </script>
    </head>
    Code (markup):
    and in the body I used:

    <body onbeforeunload="return confirmExit()">
    <center>
    <a href="http://....ic.php"><img src="http://....kt.gif" onclick="exit=false"></a>
    <a href="http://....kt.php"><img src="http://....rb.gif" onclick="exit=false"></a>
    <a href="http://....in.php"><img src="http://....se.gif" onclick="exit=false"></a>
    </center>
    Code (markup):
    How can I do this in Safari/Chrome?
    Thanks for help!
    Greetings
     
    matrixxx68, Sep 30, 2012 IP
  10. Alice carver

    Alice carver Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #10
    window.onbeforeunload = function(e) {
    e.returnValue="My custom message";
    window.setTimeout(function() {
    window.location="http://www.google.com";
    }, 2000);
    };
     
    Alice carver, Dec 28, 2015 IP