How to fire “onload” event on document in IE

前端 未结 3 340
自闭症患者
自闭症患者 2021-01-18 06:16

I am currently developing Unit Tests for a Javascript method that detects the readiness of the document. This code is already at framework level, so please avoid mentions of

相关标签:
3条回答
  • 2021-01-18 06:49

    According to this page at MSDN, there's no onload event for document.

    You want either window.onload or document.body.onload. These are identical in IE: for historical reasons, <body onload="..."> actually sets window.onload, so MS decided to make document.body.onload an alias of window.onload.

    The problem with this is - as Eric mentioned in the comments - that there doesn't seem to be a way to manually fire window events, which means that there might not be a solution for Eric's problem.

    0 讨论(0)
  • 2021-01-18 07:00

    For some reason, it appears that IE overrides the onload property of window with an empty object after the DOM is loaded. At least that is the case when you try to access it from within any event handler of a DOM element...

    <!DOCTYPE html>
    <html>
      <head>
        <title>Test by Josh</title>
        <script type="text/javascript">
          window.onload = function() {
            alert("Test");
          }
          alert(typeof window.onload);
        </script>
      </head>
      <body>
        <h1 onclick="alert(typeof window.onload);">Test</h1>
      </body>
    </html>
    

    In this situation, you'll see that window.onload is recognized as a function initially, then you see the "Test" alert. When you click on the heading, you'll see that window.onload is now an object. I tried iterating through the properties of the object, but it's empty. This is not cool.

    One lame workaround is to grab the function in the accessible scope and assign it to a different property that you can fire at your convenience...

    <!DOCTYPE html>
    <html>
      <head>
        <title>Test by Josh</title>
        <script type="text/javascript">
          window.onload = function() {
            alert("Test");
          }         
        </script>
      </head>
      <body>
        <h1 onclick="window.onloadfix()">Test</h1>
        <!-- Could potentially be injected via server-side include if needed -->
        <script type="text/javascript">
          window.onloadfix = function() {
            window.onload();
          }
        </script>
      </body>
    </html>
    

    I can't think of any other way to address this issue right now.

    0 讨论(0)
  • 2021-01-18 07:03

    The load event will fire when the document (including external resources such as images) has fully loaded, and not before.

    What results are you getting from your attempts to fire readystatechange? Does the readyState value actually change at all? Whether or not, that's not of much use either: either you fire the event with a readyState that hasn't changed, or you do so with a readyState that isn't a valid reflection of the state of the document.

    0 讨论(0)
提交回复
热议问题