Hi ,
I am trying to detect browser close and refresh event when user clicks on browsers button for refresh and close, but still not get any sloution.
My project is in PHP. i try the folling code : -

  window.onbeforeunload = function(event) {
     alert("dsghjkadghjl");
  }

  $(function () {
          $("a").click(function() {
            window.onbeforeunload = null;
          });
        });

         $(document).bind('keypress', function(e) {
            if (e.keyCode == 116){
              window.onbeforeunload = null;
            }
            else if(e.keyCode==8)
            {
             window.onbeforeunload = null;
            }
         });
         $("input[type=submit]").bind("click", function() {
             window.onbeforeunload = null;
        });

at alert i want to show the servey popup. but at the close on window not on refresh or nevigation between site pages or submit button. i able to handle the f5 and ctrl-r and a tag.

but still i am not able to detect refresh or close event of the browser. Please Help .

Member Avatar for LastMitch

how to detect browser close and refresh event

I don't think you can detect a browser like what you mention.

I mean you can used javascript to open and close a browser but detect to alert when it is close?

If you do this, you will see a downgrade in your SER, traffic loss, and zero customer retention
nobody wants popup anything, especially when they are exiting
better is a styled popover, onload, that asks the client if they WANT to answer your survey, that disappears and stays gone on NO, or if focus is transferred to any other element on the page

If you do this, you will see a downgrade in your SER, traffic loss, and zero customer retention nobody wants popup anything, especially when they are exiting

I encourage you to type a letter in the box below and try to navigate away from this page.

the prompt on DaniWeb is not a survey,
during a submission that the poster has begun, a confimation that the poster wishes to discard what may be a significant amount of effort and thought.
note a difference in intent, to confirm a user wants to discard their input
Search spiders are intelligent enough to determine code value within limitations, anything that can be considered malignant code,
New reality show, Are you smarter than a WebSpider? dont think it will happen

Goto MS, Apache, and you get an initial popover layer that asks you if you wish to complete their surveys at the completion of your task, the choice is permananent in the session

To tell you the truth, I don't see the difference...
[?] What webspider!

about the web spider, I dunno, pebkac error dont remember why I typed that, what it referred to, must have been something, but its gone now

the difference, DaniWeb's prompt,
somebody spent half an hour debugging somebody else's code, the solution isnt lost, its for the user's benefit
a different thing entirely than stopping a person navigating away from the page, for the website's benefit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.