onload

onload

load イベント

[20] load イベントは、様々な場面で発火されます。 次の各項を参照してください。

[22] dispatch でのイベント親の決定では、特別な扱いがされることもあります。

onload 属性

[1] HTMLbody 要素と frameset 要素の onload 属性は、 UAまたはすべてのフレームを読込み終えた時に発生する事象に関する内在事象属性です。

[2] 仕様書:

代替

[3] 内在事象属性は表現と構造の分離の原則に反するので、 好ましくないと考える人もいます。また、 DOM 0 に基づく古い設計です。

代わりに、 script 要素などにより関連付けたスクリプト内で DOM 2 以上の事象に関するメソッド等が利用できます。 DOM 水準2 や DOM 水準 3 の HTML 事象モジュールはこの属性に対応する DOM の事象 load を用意しています。

属性値

[4] この属性の値は %Script です。 SGML 的には CDATA です。スクリプト言語は、 Content-Script-Type によります。

[5] この属性は省略可能です。

他との関係

[6] この属性に対応する DOM の事象は load です。そちらもあわせてごらんください。

[7] 反対の意味の onunload 属性もあります。

[8] 新しい窓の読込み時の動作を規定する例 (JavaScript) HTML 4 18.2.3

<SCRIPT type="text/javascript">
      function my_onload() {
         . . .
      }
      var win = window.open("some/other/URI")
      if (win) win.onload = my_onload
</SCRIPT>

なんで HTML 4 仕様書がわざわざこんな例を interesting とかいって取り上げているのか謎です。 DOM 水準 0 では window.onloadbody 要素の onload に access できるのでしょうか?

新しい窓の読込み時の動作を規定する例 (Tcl) HTML 4 18.2.3

 <SCRIPT type="text/tcl">
     proc my_onload {} {
       . . .
     }
     set win [window open "some/other/URI"]
     if {$win != ""} {
         $win onload my_onload
     }
 </SCRIPT>

それに、 open したあとに onload を設定しても、場合によっては既に読込みが終わっているのではないかと思うのですが、 DOM 水準 0 ブラウザの実装との絶妙なバランスによって正しく動作していたのでしょうかねぇ。

歴史

[9] Internet Explorer の JavaScript の脆弱性に関する注意喚起 <http://www.jpcert.or.jp/at/2005/at050010.txt> (名無しさん 2005-11-23 00:41:40 +00:00)

[11] Bug 225244 &#8211; onload attribute not allowed in HTML tags other then BODY and FRAMESET (2007-02-28 19:55:36 +09:00 版) <https://bugzilla.mozilla.org/show_bug.cgi?id=225244> (名無しさん)

[12] A blog? with Σαιτω - onload と Safari (2007-06-15 18:42:57 +09:00 版) <http://d.hatena.ne.jp/saiton/20070615/1181861435> (名無しさん 2007-06-15 12:36:39 +00:00)

[13] Safari and page load timing (2007-06-16 11:18:57 +09:00 版) <http://www.howtocreate.co.uk/safaribenchmarks.html> (名無しさん 2007-06-16 02:24:23 +00:00)

HTML5

[21] script 要素event 属性は、正式に廃止されました。その値 onload の扱いはスクリプトの準備を参照。

[15] [whatwg] Load events fired during onload handlers ( ( 版)) <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-November/038192.html>

[16] Bug 23508 – Browsers want to know when a page's scripts have finished 'loading', even if the scripts aren't delaying the 'load' event any more (e.g. they do computation on startup) ( ( 版)) <https://www.w3.org/Bugs/Public/show_bug.cgi?id=23508>

[17] Web Applications 1.0 r8287 Clean up how we describe the 'delay the load event' logic for loading nested iframes ( ( 版)) <http://html5.org/tools/web-apps-tracker?from=8286&to=8287>

[18] Web Applications 1.0 r8407 Make nested browsing contexts delay the load event of their parent while they're loading ( ( 版)) <http://html5.org/tools/web-apps-tracker?from=8406&to=8407>

[19] svg2: changeset 697:22d3225a3ffb ( ( 版)) <https://dvcs.w3.org/hg/svg2/rev/22d3225a3ffb491aaa94e54bb709668ce005e2f6>

[23] Regression fix: run iframe load event steps immediately (annevk著, ) <https://github.com/whatwg/html/commit/3784c5375a36e19373982984ff761b491125a5da>

[24] "iframe load event steps" are run in parallel, but that seems really bad · Issue #3357 · whatwg/html () <https://github.com/whatwg/html/issues/3357>

[25] Regression fix: run iframe load event steps immediately by annevk · Pull Request #3359 · whatwg/html () <https://github.com/whatwg/html/pull/3359>

[26] Make <frame> fire load synchronously with "completely loaded" (annevk著, ) <https://github.com/whatwg/html/commit/16de123059bd4b3660677fb4fdc03e2053815482>

[27] frame and iframe have different load-event-firing behaviors · Issue #3492 · whatwg/html () <https://github.com/whatwg/html/issues/3492>

[28] Make <frame> fire load synchronously with "completely loaded" by annevk · Pull Request #3495 · whatwg/html () <https://github.com/whatwg/html/pull/3495>