Merge pull request #141 from nhooyr/patch-1

Safari does not return an empty string when a prompt is cancelled
This commit is contained in:
Ilya Kantor 2017-08-18 12:09:49 +02:00 committed by GitHub
commit 25e97bb50e

View file

@ -9,23 +9,17 @@ if (userName == 'Admin') {
if (pass == 'TheMaster') { if (pass == 'TheMaster') {
alert( 'Welcome!' ); alert( 'Welcome!' );
} else if (pass == null || pass == '') { // (*) } else if (pass == null) {
alert( 'Canceled.' ); alert( 'Canceled.' );
} else { } else {
alert( 'Wrong password' ); alert( 'Wrong password' );
} }
} else if (userName == null || userName == '') { // (**) } else if (userName == null) {
alert( 'Canceled' ); alert( 'Canceled' );
} else { } else {
alert( "I don't know you" ); alert( "I don't know you" );
} }
``` ```
Please note the `if` check in lines `(*)` and `(**)`. Every browser except Safari returns `null` when the input is canceled, and Safari returns an empty string. So we must treat them same for compatibility. Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.
Also note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.