Zjoia
Zjoia

Reputation: 176

JavaScript window.location.replace works in Firefox but not IE or Chrome

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

Upvotes: 0

Views: 9723

Answers (2)

XZILER8
XZILER8

Reputation: 1

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

Upvotes: 0

Harsh Baid
Harsh Baid

Reputation: 7249

you can do it like this also

window.location.href = 'http://myKoolUrl';

Upvotes: 2

Related Questions