Ashton Ashton - 5 months ago 42
Javascript Question

Not allowing a prompt to be closed

Is there any way to prevent a user from closing a prompt given in Javascript?

I have tried to test if the variable is

but that does not seem to work (or I can not figure it out) because once closed I am assuming it does not get to my conditional statement. Is there a way to disable the
option in the prompt message?

I am trying to make the user enter a response in the prompt message box. I am mostly a C# developer and am learning JS. I do not know how to test this in JS. Forgive me for being a tad confused.

Any help? Thank you all for reading.

my code:

var login = prompt("Who is this?");

if((login.toUpperCase()) !== "password" || (login === null)) {
} else {
alert("Access Granted.");


If you user closes the dialog then login is null.

The condition if ((login.toUpperCase()) !== "password" || (login === null)) { will first evaluate login.toUpperCase(), but because login is null it will throw the exception:

Uncaught TypeError: Cannot read property 'toUpperCase' of null(…)

You need to test if login === null first:

if ( (login === null) || (login.toUpperCase()) !== "password") {