HTML onload 属性

前へ 全ての HTML 属性 次へ

定義と用法

onload 属性は、オブジェクトがロードされたときに発火します。

onload は、web が全てのコンテンツ (画像、スクリプト・ファイル、CSS ファイルなどを含む) を完全にロードした後で、 スクリプトを実行するために <body> 要素内で最もよく使われます。 しかし、他の要素にも使用することができます(下の「HTML タグ」を参照)。

input 要素の場合、onload 属性は、<input type="image"> のときのみがサポートされます。

onload 属性は、訪問者のブラウザの種類とブラウザのバージョンを確認し、 その情報に基づき web ページの適切なバージョンをロードするのに使用することができます。


適用

onload 属性は イベント属性 の一つで、次の要素で使用することができます:

要素
<body>
<iframe>
<img>
<input>
<link>
<script>
<style>

Body の例

ページがロードされた直後に JavaScript を実行します:

<body onload="myFunction()">
Try it Yourself ❯

Img の例

<img> 要素へ onload イベントを使用します。画像がロードされた直後に "Image is loaded" をアラートします:

<img src="w3html.gif" onload="loadImage()" width="100" height="132">

<script>
function loadImage() {
    alert("Image is loaded");
}
</script>
Try it Yourself ❯

ブラウザ・サポート

onload 属性は、要素ごとに以下のブラウザがサポートしています。

要素
body Yes Yes Yes Yes Yes
iframe Yes Yes Yes Yes Yes
img Yes Yes Yes Yes Yes
link Yes Yes Yes Yes Yes
script Yes Yes Yes Yes Yes
style Yes Yes Yes Yes Yes

前へ 全ての HTML 属性 次へ