Adding a message with JavaScript before leaving the page

June 20th, 2014 by Alex Leave a reply »

It is very very easy, the callback you add should return a string that will be prepended to the confirmation message. Plain javascript:

window.onbeforeunload = function(){
  return 'You are about to lose your changes!';
};

Or with jQuery if you wish so

$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

1 comment

  1. xbox360 says:

    Wow! Finally I got a weblog from where I be able to actually obtain helpful data regarding my study and knowledge.