Javascript 按星期几进行条件重定向

Javascript 按星期几进行条件重定向,javascript,redirect,shopify,Javascript,Redirect,Shopify,我正在寻找一种基于一周中的某一天将一个页面重定向到另一个页面的方法。我只希望有问题的页面重定向从东部标准时间周日上午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: 'htt

我正在寻找一种基于一周中的某一天将一个页面重定向到另一个页面的方法。我只希望有问题的页面重定向从东部标准时间周日上午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>

常量规则=[
{日期:0,url:'https://www.whitehorsewine.com/pages/our-kitchen-1'},//重定向到星期天
{日期:1,url:'https://www.whitehorsewine.com/pages/our-kitchen-1'},//重定向到星期一
{日期:2,从:0到:11,url:'https://www.whitehorsewine.com/pages/our-kitchen-1“},//重定向到星期二下午2点
]; 
函数getRedirectUrl(){
const now=新日期();
const today=now.getDay();
const hours=now.getHours();
const first=rules.find(项=>
(item.day==未定义| | item.day===今天)和
((item.from==未定义的| | item.from>=小时)和&(item.to==未定义的| | item.to<小时))
); 
返回first.url;
} 
函数重定向(){
window.location.href=getRedirectUrl();
} 
不确定这是否重要,但我正在Shopify的主题编辑器中工作。

你离我太近了

您的比较运算符正好相反

因此,根据您的结构和显示的数据,不可能找到一个既小于
0
又大于
11
的时间

所以只要改变

item.from>=小时
item.from小时

我还要在返回和重定向逻辑中添加一些保护语句。还要确保您正在某处调用
redirect()
。我没有在您的示例中看到它,这也是它不运行的另一个原因

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)) 
); 
  if (first === undefined) { return undefined } // Guard to return undefined instead of erroring out if it can't find a matching rule
  return first.url; 
} 

function redirect() { 
  let url = getRedirectUrl()
  // Guard to catch the undefined and not call the redirect on an undefined resource.
  if ( url !== undefined ) { window.location.href = url }
} 

redirect() // Added the call to redirect so the code actually gets run.
const规则=[
{日期:0,url:'https://www.whitehorsewine.com/pages/our-kitchen-1'},//重定向到星期天
{日期:1,url:'https://www.whitehorsewine.com/pages/our-kitchen-1'},//重定向到星期一
{日期:2,从:0到:11,url:'https://www.whitehorsewine.com/pages/our-kitchen-1“},//重定向到星期二下午2点
]; 
函数getRedirectUrl(){
const now=新日期();
const today=now.getDay();
const hours=now.getHours();
const first=rules.find(项=>
(item.day==未定义| | item.day===今天)和
((item.from==未定义的| | item.from小时))
); 
if(first==undefined){return undefined}//Guard返回undefined,而不是在找不到匹配规则时出错
返回first.url;
} 
函数重定向(){
让url=getRedirectUrl()
//保护以捕获未定义的资源,而不是对未定义的资源调用重定向。
如果(url!==未定义){window.location.href=url}
} 
redirect()//添加了对redirect的调用,以便代码实际运行。
你离得太近了

您的比较运算符正好相反

因此,根据您的结构和显示的数据,不可能找到一个既小于
0
又大于
11
的时间

所以只要改变

item.from>=小时
item.from小时

我还要在返回和重定向逻辑中添加一些保护语句。还要确保您正在某处调用
redirect()
。我没有在您的示例中看到它,这也是它不运行的另一个原因

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)) 
); 
  if (first === undefined) { return undefined } // Guard to return undefined instead of erroring out if it can't find a matching rule
  return first.url; 
} 

function redirect() { 
  let url = getRedirectUrl()
  // Guard to catch the undefined and not call the redirect on an undefined resource.
  if ( url !== undefined ) { window.location.href = url }
} 

redirect() // Added the call to redirect so the code actually gets run.
const规则=[
{日期:0,url:'https://www.whitehorsewine.com/pages/our-kitchen-1'},//重定向到星期天
{日期:1,url:'https://www.whitehorsewine.com/pages/our-kitchen-1'},//重定向到星期一
{日期:2,从:0到:11,url:'https://www.whitehorsewine.com/pages/our-kitchen-1“},//重定向到星期二下午2点
]; 
函数getRedirectUrl(){
const now=新日期();
const today=now.getDay();
const hours=now.getHours();
const first=rules.find(项=>
(item.day==未定义| | item.day===今天)和
((item.from==未定义的| | item.from小时))
); 
if(first==undefined){return undefined}//Guard返回undefined,而不是在找不到匹配规则时出错
返回first.url;
} 
函数重定向(){
让url=getRedirectUrl()
//保护以捕获未定义的资源,而不是对未定义的资源调用重定向。
如果(url!==未定义){window.location.href=url}
} 
redirect()//添加了对redirect的调用,以便代码实际运行。

它现在在做什么?这与您预期的情况有什么不同?我在周一访问了该页面,但它没有重定向到新的URL。它现在在做什么?这与您预期的情况有什么不同?我在周一访问了该页面,但它没有重定向到新的URL。谢谢!这是我第一次尝试将一个脚本拼凑在一起,而不仅仅是对现有脚本进行小的编辑,所以我处于一个未知的领域。真的很感谢你的鼓励!非常感谢。这是我第一次尝试将一个脚本拼凑在一起,而不仅仅是对现有脚本进行小的编辑,所以我处于一个未知的领域。真的很感谢你的鼓励!