I have seen many times (for example: SO) on SO where people attach a load
event listener via document
, like:
document.addEventListener("load", function(){
console.log("Loaded.");
});
I can't get it. Am I right in thinking that this is not a correct use? And the proper way of using the load
event is through the window
only:
window.addEventListener("load", function () {
console.log("Loaded.");
});
Here's a simple example I tried (FF, Chrome) and it didn't work:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
document.addEventListener("load", function(){
console.log("Hello world!");
});
</script>
</head>
<body>
<h1>This is a page title</h1>
</body>
</html>
Thanks in advance!