基于日期的JavaScript重定向 [英] Javascript redirect based on date

查看:101
本文介绍了基于日期的JavaScript重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户在本月的第一个月被引导到bar.html,本月二日是gjb.html,本月三日是guerr.html,其他日期是error.html。 p>

我做错了什么?以下内容仅加载bar.html,无论用户电脑上的日期如何。

 < html> 
< script type =text / javascript>
currentTime = new Date();
if(currentTime.getDate()= 1)
window.location =bar.html;
else if(currentTime.getDate()= 2))
window.location =gjb.html;
else if(currentTime.getDate()= 3))
window.location =guerr.html;
else window.location =error.html;
< / script>
< / html>

我是全新的,所以解释一下就像一个完全白痴。 / p>

解决方案

只需要一个适当的平等检查,而不是您使用的赋值运算符:

 < html> 
< script type =text / javascript>
var currentDate = new Date()。getDate();
if(currentDate === 1)
window.location =bar.html;
else if(currentDate === 2))
window.location =gjb.html
else if(currentDate === 3))
window.location =guerr.html;
else window.location =error.html;
< / script>
< / html>

我建议 === over == 因为这是一个正确的类型检查,你保证获得整数,所以这是一个更安全的检查。当有疑问时, ===


I want the user to be directed to bar.html on the first of the month, gjb.html on the second of the month, guerr.html on the third of the month and error.html on other dates.

What am I doing wrong? The following only loads bar.html regardless of the date on the user's computer.

<html>
<script type="text/javascript">
    currentTime = new Date();
    if (currentTime.getDate() = 1)
        window.location = "bar.html";
    else if (currentTime.getDate() = 2))
        window.location = "gjb.html";
    else if (currentTime.getDate() = 3))
        window.location = "guerr.html";
    else window.location = "error.html";
</script>
</html>

I'm brand new to all this, so explain it like you would to a total idiot.

解决方案

Just needs to be a proper equality check, not the assignment operator you are using:

<html>
<script type="text/javascript">
    var currentDate = new Date().getDate();
    if (currentDate === 1)
        window.location = "bar.html";
    else if (currentDate === 2))
        window.location = "gjb.html";
    else if (currentDate === 3))
        window.location = "guerr.html";
    else window.location = "error.html";
</script>
</html>

I suggest === over == because that does a proper type check, and you are guaranteed to get Integers so this is a safer check. When in doubt, ===.

这篇关于基于日期的JavaScript重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆