Alert, Confirm, and Prompt


範例:在本範例中,我們介紹三個最常用的對話視窗。其中,我們曾經介紹過 alert() 以及 prompt()。在操作這個範例時,如果出現"取消"按鈕,請記得也 點選它,並觀察結果有何不同。如果你想不斷重複操作,你只需要在瀏覽器上點選 "重載"即可。


原始碼:

<script language="javascript">
<!--

function getinput()
{
  var res;
  var conf;
  var res = prompt("Please enter your name.", "");

  if (res != null)
  {
    if (res != "")
    {
      conf = confirm("Your name is " + res);
      if (conf == true) 
        document.write("Welcome, " + res);
      else
        document.write("See you next time.");
    }
    else
      document.write("Hello whoever you are.");
  }
  else // if Cancel was clicked
  {
    document.write("See you next time.");
  }
}

getinput();

// -->
</script>

說明: