Leron
Leron

Reputation: 9866

The value from a prompt box when ESC is pressed

I have this simple function

function Login()
{
var x=prompt("Please enter your name","");
var xmlhttp;
if (window.XMLHttpRequest)
  {// Използваните браузъри
  xmlhttp=new XMLHttpRequest();
  }
else
  {// Кой ли ползва тези версии..
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
    xmlhttp.open("GET","login.php?u="+x,true);
    xmlhttp.send();
    }

The problem is when the user decide to exit the prompt box by clicking ESC.Can someone explain to me what exactly happen with the variable x in this case.I get to the conclusion that it get assignied with the value 'null' and by null I mean a string, because when I check with

If(!is_null($u))

my script doesn't work, but if I replace this with

If($u!='null')

then everything works just fine, so could someone explain me what in fact is happening with the prompt box value when you exit it with pressin ESC?

Upvotes: 0

Views: 994

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

It returns as if you had clicked cancel.

It is null not as a string ..

alert( prompt('') === null );

will alert true if you press Esc or cancel button

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074266

x will receive the null value when the user cancels the prompt, so:

var x=prompt("Please enter your name","");
if (x === null) {
    // User canceled
}

Live example

Upvotes: 1

Related Questions