index.html window.onload во внешнем скрипте игнорируется в Javascript
<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);
//};
- Когда
window.onload
используются в HTML,window.onload
от внешнего JS будет игнорируются. - Когда
window.onload
от внешних JS закомментирована,bar.length
возвращает 0. - Когда
window.onload
из HTML удаляется,window.onload
от внешних JS работает отлично.
Может кто-нибудь объяснить, почему я не могу использовать оба window.onload
?
Если я должен был использовать window.onload
в html, как определить, загружено ли окно из внешних js?
Почему бы вам не попробовать использовать другой текст предупреждения Funtion для внешний файл js, чтобы узнать, откуда он загружен? – Si8