简体   繁体   中英

How to use onload and onunload in a same javascript program?

My code is:-

<html>
<head>
    <script>
        function welcome()
        {
            alert("Successfully loaded");
        }
        function bye()
        {
            alert("Unload");
        }
    </script>
</head>
<body onload="welcome()" onunload="bye()">

</body>

when i execute this only the function for "onload()" is called. Can you tell me how to trigger "onunload()" event also...

It really depends on what browser you test it

The onunload event is supported in IE, Firefox, and Safari, but not supported properly in Chrome or Opera.

please try onbeforeunload

<body onload="welcome()" onbeforeunload="bye()">

</body>

https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM