【问题标题】:Redirection based on URL - JavaScript基于 URL 的重定向 - JavaScript
【发布时间】:2013-08-02 17:16:42
【问题描述】:

如果有人输入“www.morgancc.edu”,我希望它重定向到我们位于“www.morgancc.edu/m”的移动网站但是,我只需要使用这个准确的重定向 em> 网址。如果您转到“www.morgancc.edu/programs”之类的内容,我不希望它重定向,这就是它目前正在做的事情。这是我到目前为止的代码:

<script type="text/javascript">
if (window.location = "www.morgancc.edu") {
   window.location.href = 'http://www.morgancc.edu/m/'; 
}
</script>

【问题讨论】:

    标签: javascript redirect


    【解决方案1】:

    location.hostname 与空路径是你似乎想要的

    if (window.location.hostname == "www.morgancc.edu" && 
        window.location.pathname=="" ) {
       window.location.href = 'http://www.morgancc.edu/m/'; 
    }
    

    另外看看href

    if (window.location.href== "http://www.morgancc.edu") {
       window.location.href = 'http://www.morgancc.edu/m/'; 
    }
    

    您可能需要在此处或那里添加一些/

    【讨论】:

    • 对我正在做的事情很有意义。但是,现在它根本不会重定向。
    • 您的第二个建议有效!这是我使用的代码: ''
    【解决方案2】:

    相等运算符是==,而不是=

    【讨论】:

      【解决方案3】:

      我建议使用服务器端脚本语言根据访问您网站的设备进行重定向。你的 if 语句中也有错字,你应该有 == 而不是 =

      <script type="text/javascript">
      if (window.location == "www.morgancc.edu") {
         window.location.href = 'http://www.morgancc.edu/m/'; 
      }
      </script>
      

      【讨论】:

      • 在我的博客网站中使用此代码。完美运行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2011-06-22
      相关资源
      最近更新 更多