JavaScript Input/Output

Objectives

  • use JavaScript's built-in alert function for output
  • use JavaScript's built-in confirm function for input/output
  • use JavaScript's built-in prompt function for input
  • use JavaScript's built-in document.write() to dynamically create HTML
  • use JavaScript's built-in document.writeln() to dynamically create HTML
  • use JavaScript's console.log() to write messages to the debugging console
  • use JavaScript's console.assert() to display error messages to the debugging console while the program is running

JavaScript I/O

  • alert(message): Used to display a message to the user.
  • confirm(message): Displays a message to the user and returns true if user clicks OK, and returns false if user clicks Cancel.
  • prompt(message, default): Used to display a message and get user input; returns the string user entered, or empty string if no string was entered, or null if user clicked Cancel.
  • Note: Some browsers (notably Firefox) have started to restrict the use of these popup dialogs somewhat and may pop up additional warning messages for the user asking if they would like to block the dialogs.
  • Note: Internet Explorer has broken some of the functionality of the prompt dialog.
  • There are workarounds for some of the broken functionality of JavaScript I/O using cross-browser JavaScript libraries.
  • document.write(string): Used to create parts of a web page dynamically by writing the HTML using JavaScript.
  • document.writeln(string): Used to create parts of a web page dynamically by writing the HTML using JavaScript. It works the same as document.write(string) except that it adds a newline after writing the string to the HTML document.
  • console.log(string): Used to display messages to a debugging console.
  • console.assert(expr): Displays an error in the debugging console if expr is false. [Note: console.assert(expr) is supported in Chrome, but may not work in other browsers.]
  • console.assert(expr, string): Displays an error including the string in the debugging console if expr is false. [Note: console.assert(expr, string) is supported in Chrome, but may not work in other browsers.]

Dialog demonstration code