简体   繁体   English

包含另一个文件时,JavaScript文件停止工作

[英]JavaScript-file stopped working when including another file

I have a own made captcha-function that uses Js and Python. 我有一个自己制作的使用Js和Python的验证码功能。 It is a riddle the user has to answer. 用户必须回答一个谜。 This worked super, but then I wanted to add a clue, if the user can not answer my question. 这超级有效,但是如果用户无法回答我的问题,那么我想添加一个线索。 So I added another file, that also works fine. 所以我添加了另一个文件,也可以正常工作。 But then my javascript handeling the communication with the python-server totally stopped working. 但是后来我的javascript处理与python服务器的通信完全停止了工作。 The window.onload wont even run. window.onload甚至不会运行。 But the file is correct included, because if I put a console.log("whatever"); 但是该文件是正确包含的,因为如果我放置console.log(“ whatever”); outside the window.onload, it prints out. 在window.onload外部,它会打印出来。

HEADER HTML (the two last includes is failing) 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>

 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(); } 

 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); } 

As for now the python-communication javascript do not work. 目前,python-communication javascript不起作用。 If i change the way the files are included in the header, the file work, but the clue-file do not. 如果我更改文件包含在标头中的方式,则文件可以工作,但是提示文件则不行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM