コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

No. 1 onloadの検証

onloadは、<body onload="xx">とするしかないと思っていたらそうでもないようで。
例えば、以下のように書ける。

<html>
<head>
<title>テスト</title>
<script type="text/javascript">
  document.onload=test();
  function test() {
    alert("1");
  }
</script>
</head>
<body>
</body>
</html>

これができると、scriptの外だしが容易になる。
また、以下のようにonloadを2つ設定しても問題ない。

<html>
<head>
<title>テスト</title>
<script type="text/javascript">
  document.onload=test1();
  function test1() {
    alert("1");
  }
  document.onload=test2();
  function test2() {
    alert("2");
  }
</script>
</head>
<body>
</body>
</html>

さらに、bodyのonloadにも追加する。

<html>
<head>
<title>テスト</title>
<script type="text/javascript">
  document.onload=test1();
  function test1() {
    alert("1");
  }
  document.onload=test2();
  function test2() {
    alert("2");
  }
  function test3() {
    alert("3");
  }
</script>
</head>
<body onload="test3()">
</body>
</html>

上記は、alertで1、2、3の順に実行された。順番が保障されているかは知らない。
I.E.7や、Firefox 2.0でどれも動いた。