根据当地时间对Javascript进行月份重定向


Month Redirect Javascript based on local time

我正在开发一个网站,并被告知根据特定月份,我们希望将用户引导到特定月份的页面。一月将转到January.html,例如,二月到二月.html。我已经创建了以下脚本,由于某些原因无法重定向页面。

如有任何帮助,我们将不胜感激!谢谢Nick

<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>

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

为了获得当前月份的数字,你可以使用这个:

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

然后你可以使用

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

按照你的意愿。

顺便说一下,使用switch而不是无数的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 */
}

也许你试图将所有事情都过于复杂。你可以用一行PHP来实现这一点。这里有一行代码可以工作。

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