JavaScript topics covered:
  1. Alert Method

    The last chapter briefly talked about the alert() method. We saw how you can display a pop up box with some message to the user. alert("I am here"); Not only can the alert() method display some text but it can also display variables. For instance, we could display a variable, or mix variables with some text.

    var pay = 500;
    	var bonus = pay * .05
    	var totalPay = pay + bonus
    
    	//display a variable
    	alert(bonus);
    	//mixing variable and text
    	alert("with my bonus I will have " + totalPay + " dollars");
    
    Click here to see the example
  2. If you notice the alert() method is just like the document.write() method exect the alert() method displays the information in a pop up dialog box. However, HTML tags like the <br> tag, does not work with the pop up dialog box. To force a new line, use escape characters.

    alert("this is line one<br>this is line two");
    alert("this is line one/nthis is line two");
    
    Click here to see the example
  3. Prompt Method

    Javascript has the prompt() method to allow you to receive information from the user. It is like the alert() method because it displays a dialog box, however it has a field on the dialog box that allows the user to type information. When the user is finished, they need to click either OK or Cancel. If the user clicks OK, then the information they typed is returned to your progam. If they click Cancel, a null value is returned to the program.

    The prompt() method has two parameters, the first is the message that displays on the dialog box, the second parameter is the default value for the entry. In the example below, "Please enter your name." is displayed on the dialog box and "your name goes here" is displayed as the default entry. The information the user types in is placed in the variable yourName when the user clicks OK. If the user clicks Cancel, yourName is set to null. Check out the demo. Try entering a name, clearing out the input area, and clicking Cancel to see what happens.

    var yourName
    yourName = prompt("Please enter your name.", "your name goes here"); 
    document.write(yourName);
    
    Click here to see the example
    ** Note: when the Cancel button is clicked, a null is placed in the variable. If no value is provided in the input, an empty sting is placed in the variable. **

    If you do not provide a default value for the second parameter, then it will be left empty. yourName = prompt("Please enter your name.");

    Click here to see the example
  4. Confirm Method

    The confirm() method allows you to ask the user if they want to continue doing something or not. It is like the prompt() method because it displays a dialog box, and it returns a value. However the user must click either OK or Cancel to continue. If the user clicks the OK button, the value true is returned to the program. If the user clicks cancel, the value false is returned to the program.

    The confirm() method has one parameter, the message that displays on the dialog box. In the example below, "Do you want to continue." is displayed on the dialog box. Check out the demo. Click OK, then refresh the screen and click Cancel to see what is returned from the confirm() method.

    var yourAnswer
    yourAnswer = confirm("Do you want to continue?"); 
    document.write(yourAnswer);
    
    Click here to see the example