Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Capture the browser close event from the close button on top left corner

Capture the browser close event from the close button on top left corner

Scheduled Pinned Locked Moved JavaScript
8 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zeyad Jalil
    wrote on last edited by
    #1

    HI, Please let me know hot can I capture the Browser close when the user close the browser from the close button on the top left side on the browser. Thanks a lot

    A F J 3 Replies Last reply
    0
    • Z Zeyad Jalil

      HI, Please let me know hot can I capture the Browser close when the user close the browser from the close button on the top left side on the browser. Thanks a lot

      A Offline
      A Offline
      Afzaal Ahmad Zeeshan
      wrote on last edited by
      #2

      There is a difference in capturing and preventing the browser from closing. Since you only want to capture, you can consider quite a few of events; such as beforeunload, and handle them to just prompt them that you are going to close the browser.

      window.onbeforeunload = function(e) {
      var dialogText = 'Dialog text here';
      e.returnValue = dialogText;
      return dialogText;
      };

      Although it may work in preventing the browser from closing as well, if you simply return null from within the function. Have a look here, [javascript - How to capture the browser window close event? - Stack Overflow](http://stackoverflow.com/questions/1631959/how-to-capture-the-browser-window-close-event) Also, in future, please also consider going through MDN documentation to see what of the APIs are available and how you can use them; [WindowEventHandlers.onbeforeunload - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload)—read the note provided there, it mentions that the calls to confirm or alert may be ignored in this event.

      The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

      J 1 Reply Last reply
      0
      • Z Zeyad Jalil

        HI, Please let me know hot can I capture the Browser close when the user close the browser from the close button on the top left side on the browser. Thanks a lot

        F Offline
        F Offline
        F ES Sitecore
        wrote on last edited by
        #3

        No event is raised that you can hook into via js.

        Z 1 Reply Last reply
        0
        • F F ES Sitecore

          No event is raised that you can hook into via js.

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          onbeforeunload works. I use it.

          There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

          F 1 Reply Last reply
          0
          • Z ZurdoDev

            onbeforeunload works. I use it.

            There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

            F Offline
            F Offline
            F ES Sitecore
            wrote on last edited by
            #5

            That doesn't just fire for closing the browser though, you don't know if it is firing due to page navigation or the browser closing.

            Z 1 Reply Last reply
            0
            • F F ES Sitecore

              That doesn't just fire for closing the browser though, you don't know if it is firing due to page navigation or the browser closing.

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #6

              True. Good clarification. I'm going to guess, and could be wrong, that the user wants to know when their app or page is closed.

              There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

              1 Reply Last reply
              0
              • Z Zeyad Jalil

                HI, Please let me know hot can I capture the Browser close when the user close the browser from the close button on the top left side on the browser. Thanks a lot

                J Offline
                J Offline
                Justiina Laukka
                wrote on last edited by
                #7

                is their any solution or any easy way by which we can make <a href="http://www.facebook.com">html</a> coding [URL=http://www.google.com]google[/URL] is their any solution or any easy way by which we can make <pre><a href="http://www.facebook.com">html</a></pre> coding <pre>[URL=http://www.google.com]google[/URL]</pre> is their any solution or any easy way by which we can make

                <a href="http://www.facebook.com">html</a>

                coding

                [URL=http://www.google.com]google[/URL]

                google Is Development The Greatest Imperative Aspect of Growth?

                1 Reply Last reply
                0
                • A Afzaal Ahmad Zeeshan

                  There is a difference in capturing and preventing the browser from closing. Since you only want to capture, you can consider quite a few of events; such as beforeunload, and handle them to just prompt them that you are going to close the browser.

                  window.onbeforeunload = function(e) {
                  var dialogText = 'Dialog text here';
                  e.returnValue = dialogText;
                  return dialogText;
                  };

                  Although it may work in preventing the browser from closing as well, if you simply return null from within the function. Have a look here, [javascript - How to capture the browser window close event? - Stack Overflow](http://stackoverflow.com/questions/1631959/how-to-capture-the-browser-window-close-event) Also, in future, please also consider going through MDN documentation to see what of the APIs are available and how you can use them; [WindowEventHandlers.onbeforeunload - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload)—read the note provided there, it mentions that the calls to confirm or alert may be ignored in this event.

                  The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

                  J Offline
                  J Offline
                  Justiina Laukka
                  wrote on last edited by
                  #8

                  We travel nationwide to deliver the best Google Analytics, Google Ad Words, and Google Tag Manager Training to digital marketers everywhere. html

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups