Ask For Confirmation When Leaving a Page

Ask for confirmation when leaving a page in javascript is useful when you have a multistep form or lengthy form that the user may expect to be saved. This code will execute whenever the page is about to be unloaded, to include clicking any links to a new page, using the forward/back buttons of the browser, and when using the address bar to go to a new site or page.

var warning = true;
window.onbeforeunload = function()
{ 
     if (warning) 
     {
    return 'You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will loose your unsaved changes';
     }
}

Of course, you can always use a cookie to save data inside of a cookie fairly easily. See how to use cookies in this article - Javascript Cookies Using jQuery Cookie Plugin