月根据当地时间重定向Javascript

时间:2013-12-02 22:20:50

标签: javascript php

我正在开发一个网站,并根据特定月份告知我们要将用户定向到月份特定页面。 1月将转到January.html,例如2月到2月。我创建了以下脚本,由于某种原因无法重定向页面。

任何帮助将不胜感激! 谢谢, 尼克

<p><script type="text/javascript">

function initArray() {
this.length = initArray.arguments.length;
for (var i = 0; i < this.length; i++)
this[i+1] = initArray.arguments[i];
}

   var MonthArray = new;
   initArray("January","February","March","April","May","June","July","July","August",
   "Septemb er","October","November","December");
var today = new Date();
var m = MonthArray[today.getMonth()+1];

var currentDate = new Date().getDate();
if (currentDate == 1)
    window.location = "CORPORATE.COM WEBSITE/folder/january.html";
if (currentDate == 2))
    window.location = "CORPORATE.COM WEBSITE/folder/february.html";
if (currentDate == 3))
    window.location = "CORPORATE.COM WEBSITE/folder/march.html";
if (currentDate == 4))
    window.location = "CORPORATE.COM WEBSITE/folder/april.html";
if (currentDate == 5))
    window.location = "CORPORATE.COM WEBSITE/folder/may.html";
if (currentDate == 6))
    window.location = "CORPORATE.COM WEBSITE/folder/june.html";
if (currentDate == 7))
    window.location = "CORPORATE.COM WEBSITE/folder/july.html";
if (currentDate == 8))
    window.location = CORPORATE.COM WEBSITE/folder/august.html";
if (currentDate == 9))
    window.location = "CORPORATE.COM WEBSITE/folder/september.html";
if (currentDate == 10))
    window.location = "CORPORATE.COM WEBSITE/folder/october.html";
if (currentDate == 11))
    window.location = "CORPORATE.COM WEBSITE/folder/november.html";
if (currentDate == 12))
    window.location = "CORPORATE.COM WEBSITE/folder/december.html";
</script></p>

2 个答案:

答案 0 :(得分:0)

我认为你的代码可能比实际需要的多。

为了将当前月份作为数字,您可以使用:

var dt = new Date();
var currentDate = currentDate.getMonth() + 1;

然后您可以使用

if (currentDate == 1) [...]

你想要的方式。

作为旁注,使用开关而不是无数if()语句会更清楚(也更恰当):

switch(currentDate){
    case 1: window.location.href='/somesite/january.html';
    case 2: window.location.href='/somesite/february.html';
    case 3: window.location.href='/somesite/march.html';
    case 4: window.location.href='/somesite/april.html';
    /* and so on */
}

答案 1 :(得分:0)

也许你正试图过度复杂化一切。 您可以使用一行PHP实现此目的。 这里有一行代码可以使用。

<?php header('Location:'."http://www.example.com/".date(F).".html");?>