为什么这个简单的(嵌套的)if 语句不起作用?

我希望实现以下目标:


如果是星期六或星期日,则显示我们已关闭页面。

如果不是星期六或星期日:

.如果在工作时间以外(不是上午 9 点到中午 12 点之间),显示我们已关闭页面。

.如果是在工作时间内,显示我们开放的页面。



这是代码:


<html>

<head>

<TITLE>Golborne Patient Booking Portal </TITLE>


<script language="JavaScript" type="text/javascript">


function ampmRedirect() {   


    var currentTime = new Date();

    var currentHour = currentTime.getHours();

    var d = new Date();

    var n = d.getDay();

    

    if (n == 0) || (n == 6){                                                    // if Saturday or Sunday

        window.location.href = "closed.html";                                   // we are closed

        } else {                                                                // if it is any other day

            if ((currentHour < 8) || (currentHour > 11)) {                      // if outside of working hours (8am to noon)

                window.location.href = "closed.html";                           // we are closed

            } else {                                                            // anything else (not weekday or within the times)

                window.location.href = "open.html";    // we are open

            }

    }

}


</script>

</head>

<body onload="ampmRedirect()">

</body>

</html>

我哪里错了?


慕码人2483693
浏览 62回答 1
1回答

开满天机

除了条件表达式周围缺少的括号外,您还可以对日期和时间进行一次检查,然后分配一个结束页面或另一个目标。function ampmRedirect() {&nbsp; &nbsp; var date = new Date(),&nbsp; &nbsp; &nbsp; &nbsp; time = date.getHours(),&nbsp; &nbsp; &nbsp; &nbsp; day = date.getDay();&nbsp; &nbsp; window.location.href = day === 0 || day === 6 || time < 8 || time > 11&nbsp; &nbsp; &nbsp; &nbsp; ? "closed.html"&nbsp; &nbsp; &nbsp; &nbsp; : "https://golborne.abtrace-cloud.com";}<body onload="ampmRedirect()">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript