window.onload no script externo é ignorado em Javascript

index.html

<html>
<head>
<script type="text/javascript" src="foo.js"></script>
<script type="text/javascript">
window.onload = function() {
    console.log("hello from html");
};
</script>
</head>
<body>
<div class="bar">bar</div>
</body>
</html>

foo.js

// this js file will be completely ignored with window.onload
//window.onload = function() {

    console.log("hello from external js");

    var bar = document.getElementsByClassName("bar");

    // this returns 0 instead of 1
    console.log(bar.length);
//};
Quandowindow.onload é usado em html,window.onload do js externo será ignorado.Quandowindow.onload do js externo é comentado,bar.length retorna 0.Quandowindow.onload de html é removidowindow.onload de js externo funciona bem.

Alguém pode explicar porque eu não posso usar os doiswindow.onload?

Se eutive usarwindow.onload em html, como saber se a janela é carregada do js externo?

questionAnswers(3)

yourAnswerToTheQuestion