Why use window.onload

Viewed 10323

I have tried finding an answer to this on my own, but only found instructions on how to use onload events. I seem to be missing the point.

I've been taught that if I want something to happen when the page loads, I should use window.onload like this:

<script>
    window.onload = dosomething();

    function dosomething()
    {
        window.alert('hello');
    }
</script>

But now that I am thinking on my own I wonder what the point of doing that is. Because this also produces the same result:

<script>
    dosomething();

    function dosomething()
    {
        window.alert('hello');
    }
</script>

Anything I put at the top inside <script> is going to execute anyway... so what's the point of window.onload?

5 Answers
Related