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

标签 javascript html css redirect dreamweaver

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

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

正如我所宣布的,这些都不起作用,为什么?

最佳答案

在纯 Javascript 中,您应该分配值位置而不是使用方法进行重定向。 replace 方法不会在历史记录中保存 url 的更改。

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>

关于javascript - 如何从其他网址自动重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46523732/

相关文章:

javascript - Jquery addClass() , removeClass() 函数不起作用

javascript - 如何勾画一个长跨度的标签?

javascript - 如何在 ProtractorJS 中扩展 ElementFinder 对象?

javascript - 如何创建一个像 Google 日历使用的那样的弹出窗口?

c# - 如何在 Visual Studio C# 中将按钮单击事件添加到我自己的按钮

javascript - 为什么这个 html 不起作用

javascript - 无法从express.js中的ace.js-div获取文本

javascript - vue合并两个对象数组

css - Angular JS 中搜索文本中的 Material 图标

html - 如何增加 mobiscroll 显示的结果