Gianluca Caliendo Gianluca Caliendo - 1 year ago 101
HTML Question

How to auto re-direct from other url?

I'm writing for a condizionale auto re-direct url, but it doesn't works. I tried with two methods:

<script>
function mobileDevice()
{
$type = $_SERVER[‘HTTP_USER_AGENT’];
if(strpos((string)$type, “Windows Phone”) != false || strpos((string)$type, “iPhone”) != false || strpos((string)$type, “Android”) != false)
return true;
else
return false;
}
if(mobileDevice() == true)
header(‘Location: https://www.organization.org/mobile/index_mobile.htm‘);
</script>


and

<script type="text/javascript">
if (screen.width <= 414) {
document.location = "index.htm";
<script language=javascript>
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
location.replace("https://www.organization.org/mobile/index_mobile.htm");
}
</script>
}
</script>


As I yet announced, no one of this works, why?

Answer Source

In plain Javascript you should assign the value location instead of using method to redirect. replace method won't save in history the change of url.

window.location = "https://www.organization.org/mobile/index_mobile.htm";

Also you are nesting two tags in your second approach. The correct code would be:

<script type="text/javascript">
      if (screen.width <= 414) {
        document.location = "index.htm";
        if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
          location.replace("https://www.organization.org/mobile/index_mobile.htm");
        }
      }
</script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download