Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions jquery.are-you-sure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down