admin管理员组文章数量:1287896
window.onload
from my reading sounds like it is loosely interchangeable with document.onload
but my experience has shown this is incorrect. I've inherited a JS script and I'm not sure how to correct it. I want the JS to execute once the DOM has loaded, not once all resources have been loaded. How can I do this?
Currently I have:
window.onload = initDropMenu;
I've tried:
document.onload = initDropMenu;
which just results in the menu not loading. I've also tried removing the line altogether from the JS and just having the DOM execute it via:
<body onload="initDropMenu()">
that also resulted in no menu, and in no errors in the console. My JS knowledge is limited, what am I missing here?
window.onload
from my reading sounds like it is loosely interchangeable with document.onload
but my experience has shown this is incorrect. I've inherited a JS script and I'm not sure how to correct it. I want the JS to execute once the DOM has loaded, not once all resources have been loaded. How can I do this?
Currently I have:
window.onload = initDropMenu;
I've tried:
document.onload = initDropMenu;
which just results in the menu not loading. I've also tried removing the line altogether from the JS and just having the DOM execute it via:
<body onload="initDropMenu()">
that also resulted in no menu, and in no errors in the console. My JS knowledge is limited, what am I missing here?
Share Improve this question asked Mar 4, 2018 at 6:43 chris85chris85 23.9k7 gold badges36 silver badges51 bronze badges 11-
1
I've never heard of
document.onload
. But<body onload=...>
should work, it's equivalent towindow.onload
. – Barmar Commented Mar 4, 2018 at 6:45 -
1
<body onload=...>
results in no menu, and no error in the console. I also had never heard ofdocument.onload
but my reading lead me to believe it existed, stackoverflow./questions/588040/…... or perhaps I misread that thread and its usage? – chris85 Commented Mar 4, 2018 at 6:47 - 2 Potentially use developer.mozilla/en-US/docs/Web/Events/DOMContentLoaded – William Fleming Commented Mar 4, 2018 at 6:54
-
1
@Barmar stackoverflow./a/588048/4333555 implies that
document.onload
can be used interchangeably withwindow.onload
.. or by my reading of it. The ment from @williamflemming has lead me to a workable answer though. I dont know enough about JS though to say if the solution is/isnt efficient. I would have thought that creating my own event listener wouldnt be needed. My current solution isdocument.addEventListener("DOMContentLoaded", function(event) {....
. – chris85 Commented Mar 4, 2018 at 7:07 -
1
I think that answer is confusing
document.onload
withdocument.onDOMContentLoaded
. Notice that it doesn't have a link fordocument.onload
, like it does forwindow.onload
. – Barmar Commented Mar 4, 2018 at 7:09
1 Answer
Reset to default 12 +200Load Event on window:
The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.
[source: developer.mozilla]
<script>
window.onload = function(){ init(); };
</script>
Load Event on HTML Elements:
The
load
event is fired when a resource and its dependent resources have finished loading.
[source: developer.mozilla]
<!-- When the image is loaded pletely -->
<img onload="image_loaded()" src="w3javascript.gif">
<!-- When the frame is loaded pletely (including all resources) -->
<iframe onload="frame_loaded()" src="about.html">
<!-- When body loaded pletely (including all resources, images and iframes) -->
<body onload="init()">
Many forums even some answers in this site may mislead you, but the load
event on body element is not only just equivalent to load
event on window, it is the exact same event. The following quote clarifies it.
For historical reasons, some attributes/properties on the
<body>
and<frameset>
elements actually set event handlers on their parent Window object. (The HTML specification names these: onblur, onerror, onfocus, onload, onscroll.)
[source: developer.mozilla]
The DOMContentLoaded Event:
What developers should use is DOMContentLoaded
event on document. It fires when the html has loaded and parsed pletely.
document.addEventListener("DOMContentLoaded", function(event) {
alert("Document is ready");
});
The DOMContentLoaded event is fired when the initial HTML document has been pletely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event load should be used only to detect a fully-loaded page. It is an incredibly popular mistake to use load where DOMContentLoaded would be much more appropriate, so be cautious.
[source: developer.mozilla]
Perhaps this is the only answer regarding this topic that has proper References
本文标签: javascriptJS windowonload Usage Vs DocumentStack Overflow
版权声明:本文标题:javascript - JS window.onload Usage Vs Document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741319471a2372119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论