私は少し JavaScript を書こうとしていますが、この分野での経験はほとんどありません。
<head> 内の <script> ブロックは <body> 内のブロックの前に実行されることが保証されていることを示唆する投稿を読みましたが、まったく逆の動作が見られます。誰かが私にこれを見ている理由を説明してもらえますか?
ここに私の簡単なテストページがあります:
<html>
<head>
<script type="text/javascript">
var test_msg;
function initMap() {
test_msg = "This is a test";
window.alert('initMap: ' + test_msg);
}
</script>
</head>
<body onload="initMap()">
<script type="text/javascript">
window.alert('blargo: ' + test_msg);
</script>
</body>
</html>
これを (Firefox または IE で) ロードすると、#1: "blargo: undefined" と #2: "initMap: this is a test" という 2 つのメッセージ ボックスが表示され、後のスクリプトが最初に実行されていることが示唆されます。
助けてくれてありがとう、
gs。