使用提示时,用户始终可以单击“取消”,并且不会返回任何值。
为了防止空值并使它更持久:
<h2>Welcome <span id="name"></span>!</h2>
<script> // 持续提示模态 var userName; while(!userName) { userName = prompt("Enter your name", ""); if(!userName) { alert("Please, we need your name!"); } else { document.getElementById("name").innerHTML = userName; } } </script>
jsFiddle演示