JavaScript数组对象在页面加载期间返回零长度,但不在控制台中返回?

时间:2014-09-18 15:37:49

标签: javascript internet-explorer safari

我正在撰写以下页面:

http://www.waisman.wisc.edu/

问题代码(内联并可在页面的第848行找到):

<script>
        var listType = "Basic",
            theList92 = document.querySelector('#block-92 > ol'),
            finalEventsList92 = "",
            futureEvents92 = [],
            pastEvents92 = [],
            nonWiley92 = [],
            events92 = [{
                title: "Golf Benefit 2015",
                date: new Date("2015, 7, 20"),
                time: "10:30 am",
                series: "Friends of the Waisman Center",
                speaker: "",
                thumbnail: "/images/Golf/GolfPhoto75x100.jpg",
                location: "Bishops Bay Country Club", url: "/events2015-GolfBenefit.htm"
            },{
                title: "David Stokes",
                date: new Date("2015, 5, 10"),
                time: "1 pm",
                series: "Children's Theatre",
                speaker: "David Stokes",
                thumbnail: "/images/CT/StokesDavidPuppets75x100.jpg",
                location: "Friends of the Waisman Center Auditorium", url: "/events-ctS2015-Stokes.htm"
            },{
                title: "&quot;The Genetics of Cerebellar Development and Function&quot;",
                date: new Date("2015, 4, 24"),
                time: "Noon",
                series: "John D. Wiley Seminar Series",
                speaker: "Dan Goldowitz, PhD",
                thumbnail: "/images/Seminars/GoldowitzDan75x100.jpg",
                location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr24-Goldowitz.htm"
            },{
                title: "&quot;The Infant Brain Imaging Study (IBIS): Insights into the Early Development of Autism&quot;",
                date: new Date("2015, 4, 17"),
                time: "Noon",
                series: "John D. Wiley Seminar Series",
                speaker: "Joseph Piven, MD",
                thumbnail: "/images/Seminars/PivenJoe75x100.jpg",
                location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr17-Piven.htm"
            },{
                title: "Snow White",
                date: new Date("2015, 4, 12"),
                time: "1 pm",
                series: "Children's Theatre",
                speaker: "PlayTime Productions",
                thumbnail: "/images/CT/PlaytimeBanner75x100.jpg",
                location: "Friends of the Waisman Center Auditorium", url: "/events-ctS2015-SnowWhite.htm"
            },{
                title: "&quot;Synaptic and Circuitry Mechanisms of Psychiatric Disorders&quot;",
                date: new Date("2015, 4, 10"),
                time: "Noon",
                series: "John D. Wiley Seminar Series",
                speaker: "Guoping Feng, PhD",
                thumbnail: "/images/Seminars/FengGuoping75x100.jpg",
                location: "John D. Wiley Conference Center", url: "/seminars-2015-Apr10-Feng.htm"
            },{}];

        events92.pop();

        if(events92.length === 0){

            finalEventsList92 = "<li>No matching events listed at this time.</li>";

        } else {
            if("Yes" === "Yes"){
                events92.forEach(function(wcEvent, index, array){
                    if(wcEvent.series !== "John D. Wiley Seminar Series"){
                        nonWiley92.push(wcEvent);
                    }
                });
                events92 = nonWiley92.reverse();
            }

            if("Future" === "Future"){
                events92.forEach(function(wcEvent, index, array){
                    if(wcEvent.date >= Date.now()){
                        futureEvents92.push(wcEvent);
                    }
                });

                futureEvents92.reverse();

                if(futureEvents92.length === 0){
                    finalEventsList92 = "<li>No matching events listed at this time.</li>";
                } else {        
                    futureEvents92.reverse();

                    for(var i = 0; i < 6; i++){
                        if(futureEvents92[i]){
                            if(listType === "Basic"){
                                finalEventsList92 += buildBasicListItem(futureEvents92[i]);
                            } else {
                                finalEventsList92 += buildListItemWithImage(futureEvents92[i]);
                            }
                        }
                    }
                }
            }
            if("Future" === "Past"){
                events92.forEach(function(wcEvent, index, array){
                    if(wcEvent.date < Date.now()){
                        pastEvents92.push(wcEvent);
                    }
                });
                if(pastEvents92.length === 0){
                    finalEventsList92 = "<li>No matching events listed at this time.</li>";
                } else {       
                    pastEvents92.reverse();

                    for(var i = 0; i < 6; i++){
                        if(pastEvents92[i]){
                            if(listType === "Basic"){
                                finalEventsList92 += buildBasicListItem(pastEvents92[i]);
                            } else {
                                finalEventsList92 += buildListItemWithImage(pastEvents92[i]);
                            }
                        }
                    }
                }
            }

            if("Future" === "All"){
                for(var i = 0; i < 6; i++){
                    if(events92[i]){
                        if(listType === "Basic"){
                            finalEventsList92 += buildBasicListItem(events92[i]);
                        } else {
                            finalEventsList92 += buildListItemWithImage(events92[i]);
                        }
                    }
                }
            }    
        }

        theList92.innerHTML = finalEventsList92;

    </script>

在初始评估events92.length === 0的所有浏览器中,在控制台中,运行相同的命令实际上给出了正确的长度(在这种情况下为66)。

此外,我知道有一些奇怪的比较(如"Future" === "Future",但该列表是由CMS生成的,这就是我们允许用户调整列表显示的方式,因为CMS不是动态的像WordPress一样。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

好吧我明白了。

我使用以下内容:

IE和Safari无法正确解析的

new Date("2014, 01, 01")。我刚刚删除了引号,一切似乎都正常工作。

奇怪的是它不会在控制台中抛出错误,但当你查看对象时它会说“无效日期”。

我也不确定为什么这会影响array.length属性,但确实如此(至少在IE / Safari中)。

对于那些可能使用相同格式的人,只需删除引号:

new Date(2014, 01, 01)