<html> <head> 〜 <script type="text/javascript" src="/sample/onload6.js"></script> 〜 </head> <body> 〜 <p id="test">〜</p> 〜 </body> </html>
function prepareOnloadFunction(func){
var oldonload = window.onload;
if(typeof window.onload != 'function'){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}
function test(){
var test = document.getElementById("test");
var testTxt = document.createTextNode("これはテストです。");
test.appendChild(testTxt);
}
prepareOnloadFunction(test);
function test2(){
var test = document.getElementById("test");
var testTxt = document.createTextNode("これはテスト2です。");
test.appendChild(testTxt);
}
prepareOnloadFunction(test2);
この関数は、ファイル読み込み後に処理をしたい関数を実行します。詳細は、DOM Scriptingを利用する上で知っておくべきことの節「使い方」以降を参照してください。
ファイルを読み込み後に関数「test」を実行する
ファイルを読み込み後に関数「test2」を実行する
それでは、本題「DOM Scriptingを利用する上で知っておくべきこと」に戻ります。