0

The following script works marvelously in FireFox but has no success at all in IE or Chrome....I've been pounding my head for hours on this stupidity....any help is appreciated.

<script type="text/javascript">
 window.onunload = function exitConfirm() 
{
    var answer = confirm("Wait don't GO! I love you!");
    if (answer)
       {
          if(!self.closed)
          {
             window.open("http://myKoolUrl");
          }else{
             window.location.replace("http://myKoolUrl");
          }
       }
 }
</script>

The Confirm works fine for both page leave and broswer/page/tab close but no matter the selection choice in IE/Chrome no redirect is taking place. Help me to understand.

Update a much more simple example using onbeforeunload:

<body onbeforeunload=go();>
function go()
{
   if(confirm("Go to Google"))
   {
     window.location.href = "http://www.google.com";
   }
}
</body>

This also does NOT work in IE/Chrome/Safari I have used a few different machines to try to eliminate setting errors of some kind. Is this just some insane situation that I am missing something obvious...??? Why is the redirect NOT doing anything in these browsers...is it just me? I've tried all the cute JS redirects:

location
location.href
location.reload
location.open
etc

even jQuery

$(location).attr('href','http://www.google.com');

Again ALL these work fine on my machine in Fire Fox

Kev
  • 118,037
  • 53
  • 300
  • 385
Zjoia
  • 176
  • 3
  • 16
  • 2
    I have to mention that this sort of behavior is very much frowned upon in "good" web design. It's generally a bad idea to second-guess the user's activities. – Ben Apr 29 '11 at 04:19
  • @Steve this is HORRIBLE for web design and generally irritating, but it makes billions of dollars a year and if these sales smucks stop making money then we stop having people to design for :-) – Zjoia Apr 29 '11 at 06:34

2 Answers2

2

you can do it like this also

window.location.href = 'http://myKoolUrl';
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
  • 1
    This is a swell way to do it but does nothing to solve my issue, the same behavior is exhibited, works in FF but not in IE/Chrome – Zjoia Apr 29 '11 at 06:35
  • I have tryed this in chrome and IE at my end it worked for me as try using this for once in your code pls `window.location.href='http://www.google.com';` – Harsh Baid Apr 29 '11 at 06:39
  • I can reproduce this on 2 different machines, maybe there is a secret conspiracy against my redirect actually working. I have implemented your solution just as you have it here http://s56.net/test.html – Zjoia Apr 29 '11 at 07:57
0

Perhaps Chrome pre-fetches the URL and knows its bogus, thus not redirecting. Trying it with a valid URL has worked for me.