tags:

views:

47

answers:

4

I am using the following simple javascript for redirection. However, it works in some machine but not others. Does anybody know why? Is there a better way to make sure every machine works? I also tried <meta> redirect it has the same issue.

<script type="text/javascript">
<!--
window.location = "http://mypage.html#instruction"
//-->
</script>
+3  A: 

I would normally use the href property of location:

<script type="text/javascript">
<!--
window.location.href = "http://www.example.com/mypage.html#instruction"
//-->
</script>

Are you getting any javascript errors?

Oded
+1  A: 

you can also try to use location.replace() method to load new url.

Lukasz Dziedzia
+2  A: 

Maybe because your URL is wrong and some browsers are being generous? http://mypage.html#instruction is probably not the URL you want. Perhaps just mypage.html#instruction without the http:// or maybe /path/mypage.html if it's on the same server, or http://server.name.com/mypage.html#instruction.

As a fallback, you should probably include HTML like

Please <a href="http://server.com/mypage.html#instruction"&gt;click here</a>

for browsers that just don't run the script for whatever reason.

Leopd
+1 One should also use a regular hyperlink as a fallback to conform with best practices.
Lèse majesté
+1  A: 

The most compatible way to do this is with a Meta refresh.

You just put this into your HTML HEAD tag:

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

I don't know why they say it is deprecated/discouraged, since it has been supported for many years and does not require any Javascript. It is discouraged because of usability, but that's another concern.

Jaanus