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);
//};
- When
window.onload
is used in html,window.onload
from external js will be ignored. - When
window.onload
from external js is commented out,bar.length
returns 0. - When
window.onload
from html is removed,window.onload
from external js works fine.
Can anyone explain why I can't use both window.onload
?
If I had to use window.onload
in html, how do tell if window is loaded from external js?