duanaoou4105 2013-12-02 22:20
浏览 34

月根据当地时间重定向Javascript

I am developing a site and have been told based on the certain month we want to direct users to month specific pages. January will go to January.html, for example, February to February.html. I have created the following script and for some reason cannot redirect the page.

Any help would be greatly appreciated! Thanks, 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>
  • 写回答

2条回答 默认 最新

  • duanjuelian4640 2013-12-02 22:28
    关注

    I think you might have more code there than you actually need.

    In order to get the current month as digits, you can use this:

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

    You can then use

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

    The way you intended.

    As a sidenote, it would be more clear (and more proper) to use a switch instead of countless if() statements:

    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 */
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看