次のコードがFirefoxとChromeで正常に実行されるのに、IE6と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>
var
しかし、前に追加するdocument.getElementById("abc");
か、名前abc
を変更するxyz
と、IE6とIE8でうまく動作します。