document.onload
Kích hoạt trước khi tải hình ảnh và nội dung bên ngoài. Sự kiện document.onload xảy ra trước window.onload.
window.onload
Kích hoạt khi trang tải xong hoàn toàn, bao gồm hình ảnh, script, CSS, v.v.
Ví dụ
Ví dụ hiểu onload:
Demo trực tiếp
<html> <head> <title>JavaScript Animation</title> <script> <!-- var imgObj = null; function init() { imgObj = document.getElementById('myImage'); imgObj.style.position= 'relative'; imgObj.style.left = '0px'; } function moveRight() { imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px'; } window.onload =init; //--> </script> </head> <body> <form> <img id="myImage" src="/images/html.gif" /> <p>Nhấn nút dưới để di chuyển ảnh sang phải</p> <input type="button" value="Click Me" onclick="moveRight();" /> </form> </body> </html>