Results 1 to 2 of 2

Thread: alert message on window close

  1. #1
    Join Date
    Oct 2012

    alert message on window close

    I have a page which have a next and previous button. when user click on next/back button, a new page will open. when ever i close the window i get the alert message. But the same message came when i click on next/previous button. I want alert message only when some one close the window.

    I am using following code.

    <script type="text/javascript" for="window" event="onunload">
    alert("The onunload event fired for the window object.");


  2. #2
    Join Date
    Jul 2007
    You have to use window.close for that.

    window.onbeforeunload = function(){
        alert('You are closing the window');
    Unload fires every time the current page is unloaded, that includes clicking on links, so you have to remove the event for them:
    $(function () {
      $("a").click(function {
        window.onbeforeunload = null;
    This snipper uses jQuery, it avoids having to recode the cross browser add event methods.
    Last edited by SparoHawk; 10-31-2012 at 01:05 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center