包含其他文件时,JavaScript文件停止工作

时间:2017-03-11 18:44:58

标签: javascript jquery python file conflict

我有一个自己的验证码函数,它使用Js和Python。这是用户必须回答的谜语。 这工作超级,但后来我想添加一个线索,如果用户无法回答我的问题。所以我添加了另一个文件,也可以正常工作。但后来我的javascript与python-server的通信完全停止了工作。 window.onload甚至都不会运行。但是文件包含正确,因为如果我放了一个console.log("无论如何");在window.onload之外,它打印出来。

HEADER HTML(最后两个包括失败)

<head>
<meta charset="UTF-8">
<title>Kristoffer Karlsson</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="css/myCSS/myStyle.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
      integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>

<script src="js/myJs/smoothScroll.js"></script>
<script src="js/jquery.touchSwipe.js"></script>
<script src="js/myJs/swipe.js"></script>
<script src="js/myJs/showContacts.js"></script>
<script src="js/myJs/showClue.js"></script>

&#13;
&#13;
window.onload = function() {
    var form = document.getElementById("jokeForm");
    console.log("contacts");
    form.onsubmit = callServer;
};


function callServer() {
    var answer = document.getElementById("answerField").value;
    console.log(answer);
    $.ajax({
        type: "GET",
        url: "http://localhost:3000/checkAnswer/"+ answer,
        success: checkAnswer
    });
    return false;
}

function checkAnswer(respons) {
    console.log(respons)
    if(respons == "wrong"){
        return false;
    }else {
        addContacts(respons);
    }
}

function addContacts(response) {
    removeQuestion();
    var parent = document.getElementById("contactInfo");
    var email = document.createElement("H2");
    var phone = document.createElement("H3");
    email.textContent = response;
    phone.textContent = "070-287 12 36";
    parent.appendChild(email);
    parent.appendChild(phone);
}

function removeQuestion() {
    var questionDiv = document.getElementById("jokeDiv");
    questionDiv.remove();
}
&#13;
&#13;
&#13;

&#13;
&#13;
window.onload = function() {
    var link = document.getElementById("giveClue");
    console.log("clue");
    link.onclick = showClue;
};

function showClue() {
    var link = document.getElementById("giveClue");
    link.style.display = "none";
    var parent = document.getElementById("jokeDiv");
    var paragraph = document.createElement("p");
    var clue = document.createTextNode("Nisse's father... who is Nisse?");
    paragraph.appendChild(clue);
    parent.appendChild(paragraph);
}
&#13;
&#13;
&#13;

至于现在的python-communication javascript不起作用。如果我改变文件包含在标题中的方式,文件可以正常工作,但是线索文件没有。

1 个答案:

答案 0 :(得分:0)

我通过添加一个带有window.onload的新文件并将其从其他文件中删除来解决这个问题。然后我将函数形式称为main javascrpt