0

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...

2
  • 1
    I'm not sure you can alert from onunload. Try onbeforeunload. Commented Apr 14, 2013 at 6:56
  • 2
    You can read more of unload and beforeunload at MDN Commented Apr 14, 2013 at 7:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well, that's an opinion that it's not supported "properly", unless there's a specification of what should or should not (be allowed to) happen in unload. Can you provide any reference to that or how Chrome/Opera differs?
using jquery is not an option?
@Mahan you can suggest, but I can't see how it could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.