在呈现内容之前触发JavaScript prompt()

时间:2016-09-17 15:38:18

标签: javascript prompt

这是我第一次使用prompt()。我已经完成了对老师给出的规范的分配,但是我很生气,在调用后台内容之前调用了prompt()(我甚至使用了window.onload = function() {blah};但是它仍然在内容之前被调用显示。

该页面的链接是http://homepage.cs.uiowa.edu/~csoultz/,然后点击lab4

这不是一个巨大的问题,而是我的好奇心。我在过去的几个小时里一直在研究它并且一无所获。我不想再加载库,如果没有库可以,请告诉我。提前感谢您的任何帮助

这是代码

<!DOCTYPE html>

<html>
<head>
  <meta charset="utf-8">
  <title>Work Week Calculator</title>
  <link rel="stylesheet" href="mySite.css">

<script>
function askquestion(question,answer){
  var userAnswer = prompt(question.toString());
  if(userAnswer!=null) return userAnswer.toLowerCase()===answer.toLowerCase();
};
</script>

</head>

<body>
  <h1>A Simple Quiz</h1>
  <p><output id="result"></output></p>
  <script>
  window.onload = function() {
    var trivia=[["Which is the most widely spoken language in the world?","Mandarin"],
                ["The art of paper folding is known as what?","Origami"],
                ["Bubble tea originated in which country?","Taiwan"]
              ];
    var correct=0;
    var total=trivia.length;
    for(i=0;i<trivia.length;i++){
      if(askquestion(trivia[i][0],trivia[i][1])){
        alert("Correct!");
        correct++;
      }
      else{
        alert("Sorry. The correct answer is "+trivia[i][1].toString());
      }
    }
    var resultElement = document.getElementById("result");
    resultElement.innerHTML = "You got "+correct.toString()+" out of "+total.toString()+" questions correct";
  };
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我能够将我的函数包装在setTimeout中,超时值为10,并且问题已在chrome中修复。

window.onload = setTimeout(function() {
                                       //Do Stuff Here
                           },10);