diff --git a/README.md b/README.md index 6a53864..9532dd5 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,23 @@ $(function() { } } +} +``` + +#### Javascript based page reloads +This plugin depends on the 'beforeunload' event being triggered on the window object to detect leaving the current page. This event will not be triggered if your application uses a AJAX Navigation javascript plugin that does not unload the window object when navigating to a different page. If the javascript plugin provides an event to notify the unload of the current page, the AreYouSure plugin can be configured to listen to that event in addition to the default `beforeunload` event using the 'softPageUnloadEvent' configuration. + +```javascript + +$(function() { + + // With additional event + $('form').areYouSure( {'softPageUnloadEvent':'your-event-name'} ); + + // Example: when using the turbolinks javascript plugin + // Refer: https://github.com/turbolinks/turbolinks#full-list-of-events + $('form').areYouSure( {'softPageUnloadEvent':'turbolinks:before-visit'} ); + } ``` The [demo page](http://www.papercut.com/products/free_software/are-you-sure/demo/are-you-sure-demo.html) diff --git a/jquery.are-you-sure.js b/jquery.are-you-sure.js index 3c41e2f..eeb3641 100644 --- a/jquery.are-you-sure.js +++ b/jquery.are-you-sure.js @@ -22,7 +22,8 @@ 'silent' : false, 'addRemoveFieldsMarksDirty' : false, 'fieldEvents' : 'change keyup propertychange input', - 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])" + 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])", + 'softPageUnloadEvent': null }, options); var getValue = function($field) { @@ -155,7 +156,7 @@ if (!settings.silent && !window.aysUnloadSet) { window.aysUnloadSet = true; - $(window).bind('beforeunload', function() { + var unload_handler = function(event) { $dirtyForms = $("form").filter('.' + settings.dirtyClass); if ($dirtyForms.length == 0) { return; @@ -168,8 +169,16 @@ window.aysHasPrompted = true; window.setTimeout(function() {window.aysHasPrompted = false;}, 900); } + if(event.type == settings.softPageUnloadEvent) { + return confirm(settings.message); + } return settings.message; - }); + } + + $(window).bind('beforeunload', unload_handler); + if (settings.softPageUnloadEvent != null) { + $(window).bind(settings.softPageUnloadEvent, unload_handler); + } } return this.each(function(elem) {