admin管理员组

文章数量:1312916

I have the following code on an html page:

<script type="text/javascript">
<!--
    window.location.replace("/");
-->
</script>

and

<meta http-equiv="refresh" content="0;url=/" >

On Google Chrome, it loads this page and then redirects to example, 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

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

I have the following code on an html page:

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

and

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

On Google Chrome, it loads this page and then redirects to example., 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.

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

Share Improve this question edited Jan 15, 2011 at 13:12 I-M-JM asked Jan 15, 2011 at 13:03 I-M-JMI-M-JM 16k26 gold badges79 silver badges105 bronze badges 3
  • And the issue is that it is not redirected or what? – Felix Kling Commented Jan 15, 2011 at 13:06
  • See, how could we have known what the issue really is? ;) But I have the feeling that Chrome implements this more correctly then the other browsers. The page containing this code is loaded anyway. Maybe the other browsers just don't render it. – Felix Kling Commented Jan 15, 2011 at 13:14
  • in my eyes, using the .replace() method for page forwarding is a dirty way. – Reporter Commented Apr 27, 2011 at 10:21
Add a ment  | 

5 Answers 5

Reset to default 2

try

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

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./");
    }
-->
</script>

Have you tried using,

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

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

Try those two methods

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

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

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);

本文标签: JavaScript redirection issue on Google ChromeStack Overflow