mardi 26 mai 2020

xmlHttpRequest is blocking other functions

I am using Js xmlHttpRequest to display the same menu on different pages of my site. Lately I found out that some functions are not executed when the site is online, like a quiz I made.

I had also tried to use fetch, or put the scripts in different files, but the same thing kept happening.

(The quiz does work when checking locally, where the xml request cannot be satisfied.)

//load the menu
onload = function loadXMLDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementsByTagName('body')[0].innerHTML +=
        this.responseText;
      }
    };
    xhttp.open("GET", "mnu", true);
    xhttp.send();
  }
  
  //check the quiz
  const checkBtn = document.getElementById('checkBtn')
checkBtn.onclick = function quizCheck() {
    //right answers
    var score = 0;
    if (q1a1.checked) {
        score = score + 1;
    }
    if (q2a1.checked) {
        score = score + 1;
    }
    alert("your score: " + score);
}
<li>
    Check the right answer:
        <br>
    <input type="checkbox" id="q1a1">Right
        <br>
    <input type="checkbox">Wrong
</li>

<li>
    Check the right answer:
        <br>
    <input type="checkbox" id="q2a1">Right
        <br>
    <input type="checkbox">Wrong
</li>

<button id="checkBtn">Check</button>

Anybody knows why and/or has some solutions?




Aucun commentaire:

Enregistrer un commentaire