我希望实现以下目标:如果是星期六或星期日,则显示我们已关闭页面。如果不是星期六或星期日:.如果在工作时间以外(不是上午 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>我哪里错了?
1 回答
开满天机
TA贡献1786条经验 获得超13个赞
除了条件表达式周围缺少的括号外,您还可以对日期和时间进行一次检查,然后分配一个结束页面或另一个目标。
function ampmRedirect() {
var date = new Date(),
time = date.getHours(),
day = date.getDay();
window.location.href = day === 0 || day === 6 || time < 8 || time > 11
? "closed.html"
: "https://golborne.abtrace-cloud.com";
}
<body onload="ampmRedirect()">
添加回答
举报
0/150
提交
取消