Reputation: 104
I'm trying to make a simple webpage with javascript. The html works fine, but I can't get the javascript to run. I wanted to know if anyone could give me an idea what was wrong with it?
The html and javascript file and are both in the same folder and I made sure I didn't do anything careless
Here's the code:
var intOne;
var intTwo;
var sec;
window.alert("Testing")
function checkAnswer()
{
if(quiz.outer.answerbox.value === intOne+intTwo)
{
alert("You smart. You loyal.");
alert("Answer is " + parseInt(intOne+intTwo));
} else {
alert("Another One.");
quiz.outer.answerbox.value="";
}
}
function displayQuestion()
{
intOne = Math.floor((Math.random() * 100) + 1);
intTwo = Math.floor((Math.random() * 100) + 1);
document.getElementById('quiz.outer.question').innerText= "What is " + intOne + " + " + intTwo + "?";
quiz.answerbox.value="";
startTimer();
}
function startTimer()
{
sec = 0;
window.setInterval(updateTime(), 1000);
}
function updateTime()
{
sec++
timer.innerText=sec;
}
<!Doctype html>
<html>
<head>
<title>Adding Quiz</title>
<script type="text/javascript" src="addingNumbers"></script>
</head>
<body onload="displayQuestion()">
<h1>Adding Quiz<h1>
<div style="color:blue">
<form name="quiz" action="#">
<p id="outer">
<p id="question">something</p>
<input type="output" id="answerbox" value=""><br>
<input type="button" value="Check" onClick="checkAnswer()">
</p>
</div>
<br>
<p>Time spent on this question so far: <strong id="timer">0</strong> seconds </p>
</body>
</html>
Oddly enough, the javascript appeared to work when i was posting the snippet, as I received an alert when I ran the code.
Upvotes: 1
Views: 10646
Reputation: 37915
Other possible answer if it's is a correct path but won't load anyway could be
content-type: text/plain
header to the script file that are beeing requested. Even if you try to add type="text/javascript"
.<base>
tag could possible change the place it looks for loading any resources (but it looks like you don't have that problem judging by your html code)<script>
tags...-
instead adding-numbers.js
instead of addingNumbers.js
(maybe some servers, filesystem, browser can have problem distinguish lowercase/uppercase and treat them them differently) I have had problem with that when using git... What happens if you try to open the script url in the browser directly?
Try using absolute path if that helps...
And of course use the console/network tab to look for what the problem could be
My guess is that it's just simply not found and that the src="addingNumbers"
is a wrong path
Are both the html and javascript file even in the same folder?
Upvotes: 2
Reputation: 207547
You have a bug in your code, but this would not keep it from running.
You are calling the method updateTime
and assigning what it returns to the Interval
window.setInterval(updateTime(), 1000);
You need to drop the ()
window.setInterval(updateTime, 1000);
Side note, intervals are not accurate for keeping time. If you're wondering why using setInterval()
is not accurate, please read this answer.
Now you need to figure out why the file is not loading. To do that you need to look at the console and the network tab. The console will show any JavaScript errors and the network tab will show if any files did not load (404 not found). As others have pointed out, you are not including a file extension in the script tag.
<script type="text/javascript" src="addingNumbers.js"></script>
Upvotes: 0