繁体   English   中英

基于URL的重定向 - JavaScript

[英]Redirection based on URL - JavaScript

如果有人输入'www.morgancc.edu',我希望它重定向到位于'www.morgancc.edu/m'的移动网站。但是,我只需要使用这个确切的网址重定向。 如果你去“www.morgancc.edu/programs”之类的东西,我不希望它重定向,这是它目前正在做的事情。 这是我到目前为止的代码:

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

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

具有空路径的位置 .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/'; 
}

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

我建议使用服务器端脚本语言来重定向访问您网站的设备。 你的if语句中也有拼写错误,你应该有== not =

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM