Beforeunload Issue

I need to make sure I get some information from a user before they close their browser.  In research, I found the on.beforeunload function, but when I implemented that function, I find that it fires everytime the user clicks on a link or refreshes the screen, etc.

Is there anyway I can limit the beforeunload function to just when the browser closes.  

Thanks.
sherbug1015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What you do is keep the onbeforeunload but then add event handlers on your <a> elements (and any other elements that causes the page to navigate away - like a form submit) and set your onbeforeunload  event handler to null

Something like this
window.onbeforeunload = function (e) {
  alert('Please ... don't kill me!');
};
$(function() {
...
  $('a').click(function() {
     window.onbeforeunload = null
  }

  $('form').submit() {
     window.onbeforeunload = null
  }
  // Other applicable event handlers here if required
}

Open in new window

0
sherbug1015Author Commented:
Thanks for your response, but I had some issues with your solution.

window.onbeforeunload = function (e) {
  return 'Mymessage';
};
$(function() {

This fires the unload message
  $('a').click(function() {  
     window.onbeforeunload = null
  });

  //$('form').submit() {        -   Removed this as it  does not compile
     //window.onbeforeunload = null
  //}
  // Other applicable event handlers here if required
});


If I could just get one event to work, I can figure out the rest.   Can you test this code and see if it works for you.  Thanks.
0
Julian HansenCommented:
Here is the code - working sample here
The sample demonstrates how this works.
$(function () {
  window.onbeforeunload = function(){
        return "Why are you leaving me ...?";
    };
  
  $('body').on('click','a.good', function(e) {
    window.onbeforeunload = null;
  });
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.