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 to window.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 of document.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 with window.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 is document.addEventListener("DOMContentLoaded", function(event) {..... – chris85 Commented Mar 4, 2018 at 7:07
  • 1 I think that answer is confusing document.onload with document.onDOMContentLoaded. Notice that it doesn't have a link for document.onload, like it does for window.onload. – Barmar Commented Mar 4, 2018 at 7:09
 |  Show 6 more ments

1 Answer 1

Reset to default 12 +200

Load 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