如何从其他网址自动重定向?

时间:2017-10-02 10:23:37

标签: javascript html css url-redirection dreamweaver

我正在写一个condizionale自动重定向网址,但它不起作用。我尝试了两种方法:

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

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

正如我所宣布的那样,没有人这样做,为什么?

1 个答案:

答案 0 :(得分:1)

在普通的Javascript中,您应该分配值位置而不是使用方法来重定向。 替换方法不会在历史记录中保存网址的更改。

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

此外,您在第二种方法中嵌套了两个标签。正确的代码是:

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