Ter*_*des 6 javascript redirect
如果有人输入'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>
Run Code Online (Sandbox Code Playgroud)
小智 6
我建议使用服务器端脚本语言根据访问您网站的设备进行重定向。你的 if 语句中也有一个拼写错误,你应该有==而不是=
<script type="text/javascript">
if (window.location == "www.morgancc.edu") {
window.location.href = 'http://www.morgancc.edu/m/';
}
</script>
Run Code Online (Sandbox Code Playgroud)
具有空路径的位置 .hostname是您想要的
if (window.location.hostname == "www.morgancc.edu" &&
window.location.pathname=="" ) {
window.location.href = 'http://www.morgancc.edu/m/';
}
Run Code Online (Sandbox Code Playgroud)
或者看看href
if (window.location.href== "http://www.morgancc.edu") {
window.location.href = 'http://www.morgancc.edu/m/';
}
Run Code Online (Sandbox Code Playgroud)
您可能需要添加一些/此处或那里