[removed] vs [removed]

后端 未结 9 1289

Which is more widely supported: window.onload or document.onload?

相关标签:
9条回答
  • 2020-11-21 06:20

    window.onload and onunload are shortcuts to document.body.onload and document.body.onunload

    document.onload and onload handler on all html tag seems to be reserved however never triggered

    'onload' in document -> true

    0 讨论(0)
  • 2020-11-21 06:23

    In Chrome, window.onload is different from <body onload="">, whereas they are the same in both Firefox(version 35.0) and IE (version 11).

    You could explore that by the following snippet:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <!--import css here-->
            <!--import js scripts here-->
    
            <script language="javascript">
    
                function bodyOnloadHandler() {
                    console.log("body onload");
                }
    
                window.onload = function(e) {
                    console.log("window loaded");
                };
            </script>
        </head>
    
        <body onload="bodyOnloadHandler()">
    
            Page contents go here.
    
        </body>
    </html>
    

    And you will see both "window loaded"(which comes firstly) and "body onload" in Chrome console. However, you will see just "body onload" in Firefox and IE. If you run "window.onload.toString()" in the consoles of IE & FF, you will see:

    "function onload(event) { bodyOnloadHandler() }"

    which means that the assignment "window.onload = function(e)..." is overwritten.

    0 讨论(0)
  • 2020-11-21 06:26

    Window.onload is the standard, however - the web browser in the PS3 (based on Netfront) doesn't support the window object, so you can't use it there.

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