msgbox codes

alert message box

alert box is used to pop up a message informing the user of something. It has no consequences and acts as a flow stopper. The program is halted by the alert box, waiting for the OK button to be clicked.

How to use alert box:
  • syntax: alert("message");
  • examples:
    as inline :
    <input type=button value="warning" onclick="alert('Your days are numbered')">
    as script:
    <script>
    <----
    function warnUser{
    alert("Up to this point is okay!");
    }
    //---->
    </script>

    Alerts are useful as greeting or warning pop-ups.
    But overuse is always annoying.
    Programmers often use alert boxes to test the flow of the script
    (for example where bugs are likely to occur).

    more faq


    prompt message box

    prompt box is used to request user to type in some text input. The input is assigned to the variable declared for the prompt box.
    How to use prompt box
  • syntax: userSay="prompt('Please type in your Say','')";click to see example
    If you want some text automatically put in the text field, put this in the second quotation set
    userSay="prompt('Please type in your Say','I say nothing!')";click to see example
  • variable as prompt input: The input from the prompt can be used as variable in the running script
    <script>
    userSay=prompt('Please type in your Say','I say nothing!');
    alert("What do you mean by '"+userSay+"' ?");
    </script>

    run this example
  • Problems with prompt message box:
    users who close the box without typing in the input will cause the prompt to return "null" as the result. This is annoying and will cause errors if an input is required. Solution: assign a valid variable to replace the null value.
    userSay=prompt("What's your name?","");
    if (userName==""|userName=="null"){userName="No Name"};


    more faq


    confirm message box

    confirm message box asks the user to either click OK or choose Cancel.
    The result is a boolean, true if OK is clicked or false if Cancel is clicked.
    How to use confirm box
  • syntax:
    confirm("condition");
  • Example: function HireFire(){
    if(confirm("Will you OK my contract or Cancel it ?")==true)
    {alert("I thank you!")};
    else{alert("You bastard!")};
    }

    run this example
  • Unlike the prompt box, confirm box never returns empty or null values. It only returns either true (1) or false (0).
  • If the box is closed (without clicking OK or Cancel) then 0 is returned.
  • It is useful in directing the user along the correct path of program flow.

    more faq


    MORE TO COME IN NEXT UPDATE
    javascript - basic stuffs