Adjust to the new behaviors of beforeunload handler

This commit is contained in:
Cheng Zhao 2016-05-23 13:28:16 +09:00
parent 06800940ec
commit fa0ce7ad5f
5 changed files with 8 additions and 29 deletions

View file

@ -26,7 +26,7 @@ void AtomJavaScriptDialogManager::RunBeforeUnloadDialog(
bool is_reload, bool is_reload,
const DialogClosedCallback& callback) { const DialogClosedCallback& callback) {
// FIXME(zcbenz): the |message_text| is removed, figure out what should we do. // FIXME(zcbenz): the |message_text| is removed, figure out what should we do.
callback.Run(true, base::ASCIIToUTF16("FIXME")); callback.Run(false, base::ASCIIToUTF16("This should not be displayed"));
} }
} // namespace atom } // namespace atom

View file

@ -217,17 +217,17 @@ will cancel the close.
Usually you would want to use the `beforeunload` handler to decide whether the Usually you would want to use the `beforeunload` handler to decide whether the
window should be closed, which will also be called when the window is window should be closed, which will also be called when the window is
reloaded. In Electron, returning an empty string or `false` would cancel the reloaded. In Electron, returning any value other than `undefined` would cancel the
close. For example: close. For example:
```javascript ```javascript
window.onbeforeunload = (e) => { window.onbeforeunload = (e) => {
console.log('I do not want to be closed'); console.log('I do not want to be closed');
// Unlike usual browsers, in which a string should be returned and the user is // Unlike usual browsers that a message box will be prompted to users, returning
// prompted to confirm the page unload, Electron gives developers more options. // a non-void value will silently cancel the close.
// Returning empty string or false would prevent the unloading now. // It is recommended to use the dialog API to let the user confirm closing the
// You can also use the dialog API to let the user confirm closing the application. // application.
e.returnValue = false; e.returnValue = false;
}; };
``` ```

View file

@ -476,18 +476,11 @@ describe('browser-window module', function () {
}) })
describe('beforeunload handler', function () { describe('beforeunload handler', function () {
it('returning true would not prevent close', function (done) { it('returning undefined would not prevent close', function (done) {
w.on('closed', function () { w.on('closed', function () {
done() done()
}) })
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-true.html')) w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
})
it('returning non-empty string would not prevent close', function (done) {
w.on('closed', function () {
done()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-string.html'))
}) })
it('returning false would prevent close', function (done) { it('returning false would prevent close', function (done) {

View file

@ -1,13 +0,0 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
window.onbeforeunload = function() {
setTimeout(function() {
require('electron').remote.getCurrentWindow().emit('onbeforeunload');
}, 0);
return 'string';
}
window.close();
</script>
</body>
</html>

View file

@ -5,7 +5,6 @@
setTimeout(function() { setTimeout(function() {
require('electron').remote.getCurrentWindow().emit('onbeforeunload'); require('electron').remote.getCurrentWindow().emit('onbeforeunload');
}, 0); }, 0);
return true;
} }
window.close(); window.close();
</script> </script>