【发布时间】:2020-06-15 14:50:31
【问题描述】:
我正在寻找一种基于星期几将一个页面重定向到另一个页面的方法。我只希望相关页面从美国东部标准时间周日上午 12:00 到美国东部标准时间周二下午 2:00 重定向。
这就是我所拥有的。当我测试它时,它没有重定向到新的 URL。
<script>
const rules = [
{ day: 0, url: 'https://www.whitehorsewine.com/pages/our-kitchen-1' }, // Redirect for Sunday
{ day: 1, url: 'https://www.whitehorsewine.com/pages/our-kitchen-1' }, // Redirect for Monday
{ day: 2, from: 0, to: 11, url: 'https://www.whitehorsewine.com/pages/our-kitchen-1' }, // Redirect for Tuesday until 2pm
];
function getRedirectUrl() {
const now = new Date();
const today = now.getDay();
const hours = now.getHours();
const first = rules.find(item =>
(item.day === undefined || item.day === today) &&
((item.from === undefined || item.from >= hours) && (item.to === undefined || item.to < hours))
);
return first.url;
}
function redirect() {
window.location.href = getRedirectUrl();
}
</script>
不确定是否重要,但我正在使用 Shopify 的主题编辑器。
【问题讨论】:
-
它现在在做什么?这与您预期的结果有何不同?
-
我周一访问了该页面,但它没有重定向到新的 URL。
标签: javascript redirect shopify