I-M-JM
I-M-JM

Reputation: 15950

JavaScript redirection issue on Google Chrome

I have the following code on an html page:

<script type="text/javascript">
<!--
    window.location.replace("http://www.example.com/");
-->
</script>

and

<meta http-equiv="refresh" content="0;url=http://www.example.com/" >

On Google Chrome, it loads this page and then redirects to example.com, while, on other browsers that I have tested (IE and Firefox), it does not load this HTML page (on which this particular code is) but directly shows up example.com

Can any one tell me what's wrong with my code and any suggestions to improve it, so that it will also work on Google Chrome too.

Thanks

Upvotes: 2

Views: 11697

Answers (5)

Nicholas Petersen
Nicholas Petersen

Reputation: 9558

In my case I was redirecting to the same page, but setting a different #target at the end of the url. Nothing was working, including returning false afterwards, but what fixed it was reloading the page after setting the URL, as follows (in this example, is only fired after a 6 second delay):

setTimeout(function () {            
    window.location.href = redirectUrl;
    window.location.reload(true);
    return false; // maybe not needed...
}, 6000);

Upvotes: 0

David Yell
David Yell

Reputation: 11855

Have you tried using,

window.location.href = "http://www.example.com/";

As per the docs, https://developer.mozilla.org/en/DOM/window.location#Properties

Upvotes: 1

Zango
Zango

Reputation: 2387

Maybe it will work as you want if you write your code like this:

<script type="text/javascript">
<!--
    window.onload = function(){
         window.location.replace("http://www.example.com/");
    }
-->
</script>

Upvotes: 1

Wasim Karani
Wasim Karani

Reputation: 8886

Try those two methods

<script type="text/javascript">
    document.location="http://www.example.com/";
</script>

<script type="text/javascript">
    document.location.href="http://www.example.com/";
</script>

Upvotes: 0

TeAmEr
TeAmEr

Reputation: 4773

try

<script type="text/javascript">
<!--
    document.location="http://www.example.com/";
-->
</script>

Upvotes: 2

Related Questions