所有元素加载后发出警报

时间:2017-01-17 02:23:28

标签: javascript html

我有一个测验,如果你得到100%,当你加载最后一页时,它会发出警报"你得到了100%!"。虽然,它在后台加载之前加载。这是我的代码:



var chanceoflive1 = parseInt(localStorage.getItem("chanceoflive1"));
    var chanceoflive2 = parseInt(localStorage.getItem("chanceoflive2"));
    var chanceoflive3 = parseInt(localStorage.getItem("chanceoflive3"));
    var chanceoflive4 = parseInt(localStorage.getItem("chanceoflive4"));
    var chanceoflive7 = parseInt(localStorage.getItem("chanceoflive7"));

    var chanceoflivefinal = (chanceoflive1||0) + (chanceoflive2||0) + (chanceoflive3||0) + (chanceoflive4||0) + (chanceoflive7||0);
    var chanceoflivepercent = chanceoflivefinal * 4;
   

function startup(){
        if (chanceoflivefinal == 25) {
            alert("You Got 100%!");
        }
    }

<body onload="startup">
<center>
    <h2 class="text">Final Results</h2>

    <p id="print" class="text"></p>
    <p id="print2" class="text"></p>

    <img src="qrcode.jpg" height="300" length="300">

    <br>

    <div class="wrapper">
        <a href="index.html">
    <button align=center onclick="handleClick()" id="button">
      <canvas width="200" height="50" id="canvas" align=center></canvas>
      <span class="text" id="submit">Retry</span>
    </button>
        </a>
    </div>
    </center>
  </body>
&#13;
&#13;
&#13;

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你总是可以在script标签内做这样的事情:

(function(){ // on document load
    alert("Ready!"); // alert "ready"
})();

哦,正如 @Barmar 所说,要调用function,您需要将startup更改为startup()

答案 1 :(得分:0)

您只是遗漏了();这样的onload="startup();"

也许你也可以使用

document.onload = startup();

甚至

window.onload = startup();
相关问题