-2

When someone tries to leave my website I want to show them pop up message. I tried to use window.onbeforeunload function but it is not working.

<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit(){
     return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }
</script>

Hi Jonathan i want to show my custom messages in pop up. currently it shows default message - This page is asking you to confirm that you want to leave - data you have entered may not be save

  • 2
    http://stackoverflow.com/questions/19263277/open-a-custom-popup-on-browser-window-tab-close – Lucky Chingi Nov 20 '15 at 05:31
  • http://stackoverflow.com/questions/16735076/popup-before-window-is-closed – Lucky Chingi Nov 20 '15 at 05:32
  • http://stackoverflow.com/questions/8118722/how-to-show-pop-up-survey-with-jquery-when-leaving-page – Suyog Nov 20 '15 at 05:33
  • http://stackoverflow.com/questions/7080269/javascript-before-leaving-the-page – Suyog Nov 20 '15 at 05:33
  • http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser – Ahsan Nov 20 '15 at 05:35
  • 2
    Can you explain what exactly the issue is? What is happening with the code you provided? Are you receiving any errors? (Are you trying to achieve something other than the snippet you've posted?) – Jonathan Lonowski Nov 20 '15 at 05:35
  • 1
    The code as quoted is fine (other than using the outdated `language` attribute, which will be ignored) and will work. [Proof](http://output.jsbin.com/piqifugiju) So the problem is in code you're not showing. – T.J. Crowder Nov 20 '15 at 05:37
  • `beforeunload` wont work in opera – coolguy Nov 20 '15 at 05:45
  • Hi Jonathan i want to show my custom messages in pop up. currently it shows default message - This page is asking you to confirm that you want to leave - data you have entered may not be save – mmthedependable Nov 21 '15 at 05:53

2 Answers2

-1

Try this.

 $(window).bind('beforeunload', function() {        
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
            }); 
Ankit Kathiriya
  • 1,221
  • 10
  • 17
-2

2nd line of your core is buggy

It should be :

window.onbeforeunload=confirmExit();

Your line is missing the parenthesis, ().

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 1
    *"Your line is missing the parenthesis ()"* And it **should** be missing them. – T.J. Crowder Nov 20 '15 at 05:36
  • 1
    Because you assign a function to the `onbeforeunload` property in order to hook the event, you don't want to **call** the function and assign its return value to the property. This is really, really basic DOM event handling. – T.J. Crowder Nov 20 '15 at 05:38