Wow, this is ridiculous. Take the following snippet and throw it in a static HTML page somewhere. Clearly it uses very poor coding style, but look at the results in Internet Explorer versus Firefox. What on earth is IE doing? If someone can explain that to me, I’d be very grateful.
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>I can't believe this happens...</title>
</head>
<body>
<p id="bialecki">This is the "bialecki" paragraph element.</p>
<p>Check out the source.</p>
<script type="text/javascript">
try {
alert(bialecki.id);
alert(bialecki.className);
} catch (e) {
alert("No, I don't define random variables.");
}
try {
// Try the following with and without the var. Wow, IE, wow...
bialecki = 3;
// var bialecki = 3;
alert("bialecki = " + bialecki)
} catch (e) {
alert("Error!")
}
</script>
</body>
The only other point is that if you “var” scope the variable, everything is fine and it behaves as expected, even though it’s still scoping the variable to the global/window scope. So what’s the point? Always use the “var” keyword even if you’re not going to scope your variables. Although you should do that too.
Social Links