Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document.onload not working in JavaScript? [duplicate]

Tags:

javascript

I write document.onload code inside my head tag.. but it doesn't seems to work. here is my code..

<head>
<script>
    document.onload = function () {
        alert("Window Loaded...!!");
    }
</script>
</head>

But if i replace with window.onload it perfectly works!! What is the problem? Am I doing something wrong ??

like image 917
Uthaiah Avatar asked Mar 20 '26 09:03

Uthaiah


1 Answers

As far as I'm aware, the closest you can come to your method is:

document.addEventListener('DOMContentLoaded', function () {
   /* your logic here */
});

The problem you have is document may not have a method of onload for a particular browser. Luckily! The window does in most cases. :) Give that a try for your JavaScript invocation.

like image 112
lindsay Avatar answered Mar 23 '26 00:03

lindsay