如何在JavaScript中检测name = prompt()的null结果? [英] How does one detect a null result from name = prompt() in JavaScript?

查看:53
本文介绍了如何在JavaScript中检测name = prompt()的null结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:



  var name = prompt(你叫什么名字,匿名); if(name == null){alert(detected null); name =Anonymous;} alert(Hello+ name);  



据我了解,单击[取消]或按[退出]键将导致JavaScript window.prompt(text,[default])函数返回 null (或者在旧版本的IE中可能 undefined



当我在Firefox中执行上述代码时,会出现预期的提示。但是,当我按[Escape]时,我从未看到检测到空消息(并且名称变量未设置为匿名。就好像名称变量未设置为 null 。有趣的是,最后一个警告显示Hello null。



我是尝试使用相同的行为交换 name == null 检查 name === null



如何在JavaScript中检测 null



请注意:我真的想要检测null,而不是空字符串。

解决方案

提示符()函数返回 null 当用户按下escape时,如最新HTML 5工作草案 Web应用程序API 部分所述:





  1. 如果用户中止,则返回null;否则,返回用户回复的字符串。


你只需要检查 result === null result == null ,就像你一样。



您遇到的行为与 prompt()无关。这是因为您尝试在全局范围内使用变量 name 。您实际上并没有声明一个新变量。您正在重复使用全局 window.name 属性,自动转换为字符串:



  var nameNumber = 21; var name = 21; document.write([typeof nameNumber,typeof name]); //数字,字符串 



如果您使用,您的代码将正常工作一个不同的变量名,或者如果你把它包装在一个函数中,那么它就不会与全局变量冲突。



  function main(){ var name = prompt(你叫什么名字,匿名); if(name == null){alert(detected null); name =匿名; } alert(Hello+ name);} main();  


Consider this code:

var name = prompt("What is your name", "Anonymous");
if (name == null) {
  alert("detected null");
  name = "Anonymous";
}
alert("Hello " + name);

It is my understanding that clicking [Cancel] or pressing the [Escape] key will cause the JavaScript window.prompt(text, [default]) function to return null (or perhaps undefined in older versions of IE)

When I execute the above code in Firefox, the expected prompt appears. However, when I press [Escape], I never see the "detected null" message (and the name variable is not set to "Anonymous". It is as if the name variable is not being set to null. Interestingly, the last alert displays "Hello null".

I've tried swapping out the name == null check with name === null with the same behavior.

How does one detect null in JavaScript?

Please note: I am really trying to detect null, not an empty string.

解决方案

The prompt() function returns null when the user presses escape, as described in the Web application APIs section of the latest HTML 5 Working Draft:

  1. If the user aborts, then return null; otherwise, return the string that the user responded with.

You just need to check that result === null or result == null, as you have done.

The behaviour you're encountering doesn't have to do with prompt(). It's because you're trying to use the variable name in a global scope. You aren't actually declaring a new variable. You're reusing the global window.name property, which is automatically converted to a string:

var nameNumber = 21;
var name = 21;
document.write([typeof nameNumber, typeof name]);  // number,string

Your code will work correctly if you use a different variable name, or if you wrap it in a function so it isn't colliding with the global variable.

function main() {
  var name = prompt("What is your name", "Anonymous");
  if (name == null) {
    alert("detected null");
    name = "Anonymous";
  }
  alert("Hello " + name);
} 

main();

这篇关于如何在JavaScript中检测name = prompt()的null结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆