Nathan Chan
Nathan Chan

Reputation: 303

Alert After All Elements Have Loaded

I have a quiz, and if you get 100%, when you load the last page, it will alert "You got 100%!". Although, it loads in before the background loads in. This is my code:

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>

How can I fix this?

Upvotes: 0

Views: 71

Answers (2)

GROVER.
GROVER.

Reputation: 4378

You could always do something like this within a script tag:

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

Oh, and as @Barmar said, to call that function, you need to change startup to startup()

Upvotes: 1

ajbee
ajbee

Reputation: 3641

You are just missing (); like this onload="startup();".

Perhaps you can also use

document.onload = startup();

or even

window.onload = startup();

Upvotes: 0

Related Questions