最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

window.onload in external script gets ignored in Javascript - Stack Overflow

programmeradmin11浏览0评论

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 pletely 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);
//};
  1. When window.onload is used in html, window.onload from external js will be ignored.
  2. When window.onload from external js is mented out, bar.length returns 0.
  3. 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?

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 pletely 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);
//};
  1. When window.onload is used in html, window.onload from external js will be ignored.
  2. When window.onload from external js is mented out, bar.length returns 0.
  3. 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?

Share Improve this question edited Apr 1, 2013 at 20:44 user1643156 asked Apr 1, 2013 at 20:33 user1643156user1643156 4,53711 gold badges41 silver badges59 bronze badges 2
  • Why don't you try to use a different text in alert funtion for the external js file, to see where it loaded from? – Si8 Commented Apr 1, 2013 at 20:37
  • Use onload in your index page – mm4096 Commented Oct 20, 2021 at 2:07
Add a ment  | 

3 Answers 3

Reset to default 17

1)The way you're binding, you can have just one method attached to an event. You need to add an event listener for what you want.

window.addEventListener("load", function() { alert("hello!");});

Setting directly a method to the onload event will replace any previously attached method. But if you use listeners instead, you can have many of them bound to an event.

2)If you ment out the onload in your external file, when the document.getElementsByClassName("bar") is called, your document isn't ready yet, then, it will return 0 items.

3)Use the addEventListener as I explained in the first point. If you apply this in both places, it will work like a charm.

onload is a property of window. It acts like any other variable property. When you try to use it twice you're overwriting the original value with your second write.

So your entire external script is ignored when you wrap it in window.onload, because window.onload is then overwritten to be

function() {
    console.log("hello from html");
};

If you want to do execute 2 functions, define 2 functions, a and b,

and set window.onload like this:

window.onload = function(){
  a();
  b();
}

Alternatively, you can bind 2 separate events in the way Alcides' answer suggests. My personal view is that its cleaner to do a single bind with multiple functions since its easier to know whats bound, know what order your functions will execute in, and see everything thats happening in one place, but its mostly a matter of style/preference if the order doesn't matter.

Thats Correct, you are overwriting your own onload, but you can always attach a new event listener to the window like this

function onLoadHandler(){
console.log("hello from external js");

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

// page not loaded, so this returns 0 instead of 1
console.log(bar.length);
}
if (window.addEventListener) {
window.addEventListener('load', onLoadHandler); }
 else if (window.attachEvent) { window.attachEvent('onload', onLoadHandler ); }
发布评论

评论列表(0)

  1. 暂无评论