Why the following code runs well in Firefox and Chrome but causes an error in IE6 and IE8?
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="abc"></div>
<script type="text/javascript">
var doLoad = function() {
// error in ie6 and ie8
abc = document.getElementById("abc");
abc.innerHTML = "hello world!";
// correct in ie6 and ie8
/*
var abc = document.getElementById("abc");
abc.innerHTML = "hello world!";
*/
// correct in ie6 and ie8
/*
xyz = document.getElementById("abc");
xyz.innerHTML = "hello world!";
*/
}
window.onload = doLoad;
</script>
</body>
</html>
But if I add var
before document.getElementById("abc");
or rename abc
as xyz
, It would runs well in IE6 and IE8.