JavaScript onload函数没有执行

时间:2014-12-20 18:46:33

标签: javascript function

我遇到了问题,所以让我们直截了当地说,我的功能并没有执行。

代码:

<body class="addbackground" onload="compareDate()">
   <script type="text/javascript">
var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
console.log("Array");

function compareDate() {
console.log("Function fired");
var today = new Date();
var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + ","+ today.getMinutes() + "," + today.getSeconds();
  if(Date.parse(todayDate) > Date.parse(season5[0])) {
    document.getElementById("testDate").innerHTML = "111";
console.log("111");
}
else {
document.getElementById("testDate").innerHTML = "222";
console.log("222");
}
}
</script>
<div id="testDate">Oeps...</div>
</body>

我必须输入更多文字等等......我还要写更多......

3 个答案:

答案 0 :(得分:1)

这是工作代码,我们需要使用getDate()代替getDay(),(today.getMonth()+ 1)并处理season5 [0]。

http://jsfiddle.net/sahilbatla/p7rfcLgt/

<script>
 function compareDate() {
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + (today.getMonth() + 1) + "," + today.getDate() + "," + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    console.log(todayDate)
    if (Date.parse(todayDate) > Date.parse('12/20/2014')) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>
<body class="addbackground" onload="compareDate()">
    <div id="testDate"></div>
</body>

答案 1 :(得分:0)

问题可能在别处。我创建了这个小提琴,告诉你函数执行正常。

See it work

代码:

         

<h1>Hello World!</h1>
<div id="testDate">Oeps...</div>
<script>
    var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
function compareDate() {
    alert("What do you mean it doesn't run??");
    console.log("Function fired");

    var today = new Date();
    var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + "," + today.getMinutes() + "," + today.getSeconds();
    if (Date.parse(todayDate) > Date.parse(season5[0])) {
        document.getElementById("testDate").innerHTML = "111";
        console.log("111");
    } else {
        document.getElementById("testDate").innerHTML = "222";
        console.log("222");
    }
}
</script>

</body>
</html>

答案 2 :(得分:-1)

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
</head>
<body onload="compareDate()">

<script>
var season5 = [
"2014,09,13,22,00,00", //1
"2014,09,20,22,00,00", //2
"2014,09,27,22,00,00", //3
"2014,10,3,22,00,00", //4
"2014,10,10,22,00,00", //5
"2014,10,17,22,00,00", //6
"2014,10,24,22,00,00", //7
"2014,11,2,22,00,00", //8
//MidSeason break
"2015,01,9,22,00,00", //9
"2015,01,16,22,00,00", //10
"2015,01,23,22,00,00", //11
"2015,02,2,22,00,00", //12
"2015,02,9,22,00,00", //13
"2015,02,16,22,00,00", //14
"2015,02,23,22,00,00", //15
"2015,02,30,22,00,00", //16
];
console.log("Array");

function compareDate() {
console.log("Function fired");
var today = new Date();
var todayDate = today.getFullYear() + "," + today.getMonth() + "," + today.getDay() + "," + today.getHours() + ","+ today.getMinutes() + "," + today.getSeconds();
  if(Date.parse(todayDate) > Date.parse(season5[0])) {
    document.getElementById("testDate").innerHTML = "111";
console.log("111");
}
else {
document.getElementById("testDate").innerHTML = "222";
console.log("222");
}
}
</script>
<div id="testDate"></div>
</body>
</script>
</html>

试试这个,它对我有用