[Solved] Document.onload not working in JavaScript? [duplicate]

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 ??

Enquirer: Uthaiah

||

Solution #1:

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.

Respondent: lindsay

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.