jQuery OnBeforeUnLoad Not Working Properly

jQuery OnBeforeUnLoad Not Working Properly

Usually when using Javascript onbeforeunload to detect page reload or page refresh events, if the refresh or reload is detected, a popup appears  displaying a message and the options to either cancel or leave the page.

The problems I'm having is number one, my custom message is not displayed but instead a default message is displayed in Chrome. And two, I would like to call a method when a user clicks the Leave Page option. 

I've read that this can be achieved using the unload function but it is not working inside of jQuery document ready. Below is what I have tried:
  1. function MyMethod(){
  2.    // do something
  3. }

  4. $(document).ready(function(){
  5.         $(window).on("unload", function () {
  6.              MyMethod();
  7.         });

  8.         $(window).on('beforeunload', function () {
  9.              return "Refreshing or leaving this page will cause you to lose data!"
  10.         });
  11. });