The following is my HTML page:
<html>
<head>
<script type="text/javascript" src="/game/js/ready/main.js" ></script>
<link rel="stylesheet" type="text/css" href="/game/css/nav.css" />
</head>
<body>
<div class="nav-buttons">
<span class="nav-button" onclick="f()">Show A</span>
<span class="nav-button" onclick="h()">Show B</span>
<span class="nav-button" onclick="g()">Show C</span>
</div>
<div id="divA">
a
</div>
<div id="divB">
b
</div>
<div id="divC">
c
</div>
</body>
</html>
And the following is my main.js file:
var divA = document.getElementById("divA");
var divB = document.getElementById("divB");
var divC = document.getElementById("divC");
function f(){
window.divA.style.display = "block";
window.divB.style.display = "none";
window.divC.style.display = "none";
}
function h(){
window.divA.style.display = "none";
window.divB.style.display = "block";
window.divC.style.display = "none";
}
function g(){
window.divA.style.display = "none";
window.divB.style.display = "none";
window.divC.style.display = "block";
}
As far as I could tell, this is valid. However, when I call f()
, I get an error saying that all of the above defined variables are null.
When changing the var
keyword to let
, the above code runs with no problems.
Which is really weird, because as far as I understood from this answer, the opposite should be the case.
Is this a misunderstand of mine, or a weird bug in my browser ?