When click on the browser closed button l want alert msg. I got a little code from net but I'm not getting any result. //I add this simple code in top of my page. <script language="javascript"> window.onunload=onBrowserClose function onBrowserClose() { alert("Window Closed"); } </script> //But problem is that when i click on the browser closed button, I'm not getting any msg but page was closed. Please help me out from this problem.
1) You're using the wrong event name (it should be onbeforeunload). 2) You have to return the message (a string) you want displayed in the event handler. Example: <script type="text/javascript"> window.onbeforeunload = function() { return "Please don't leave!"; } </script> Code (markup): I'm not sure which browsers support the onbeforeunload event; I don't think all of them do.
The first line of the above code contains two errors: 1. There is no event called onwindowunload, so the code as written creates a new variable with that name. This part of the statement should be changed to window.onunload 2. The parentheses need to be removed from the right side of the assignment. As written, the function is called as soon as the page loads (displaying the alert message before displaying the page's content) and its return value (undefined) is what gets assigned. A working version of the code would look like this: window.onunload = browserclose; function browserclose() { alert("message"); } Code (markup): Ironically, that is programmatically identical to Subikar's original code. Subikar, what browser are you using to test it out on? It might not be supported by the browser. Your original code works for me in IE.