|
All this does is tell the computer the language that is going to be used. You must use this tag, <script>, or <script = "JavaScript"> to make this Prompt or any other JavaScript work.
<!-- Begin hiding here --Since some browsers can't read JavaScript, they just read over the JavaScript code as text and show it to the user as text. This fragment of code begins the commenting out of this text if the user's browser cannot read JavaScript.
/*******************************************************************************************These are a bunch of comments which must be left in here if you use this script. The /*,anything after it and before */ are comments. When the computer gets to this part, it skips over it.
var name = prompt("Welcome To JCL's Tutorial On JavaScript Prompts. Please Enter Your Name:","Anonymous Web Cruiser");This command assigns the variable (var = variable), name to the returning value of the predefined function, prompt(). To get the returning value of the prompt() function, the computer activates it. The function prompt() creates a box which prompts the user for input. The text inside of its parentheses is used for the message to the visitor and the text that should go in the input field. The message to your visitor goes first followed by a comma and the text that should appear in the input field. If the programmer does not specify the text that should appear in the input field, <undefined> appears in the input box, and if he/she does not specify the message to the user, "undefined" becomes the message. If you want to make either the message or input field blank, use "" in the appropriate place (The message goes before the comma and the input field text goes after). The prompt's returning value is the user's input so name equals the text that was typed in by the visitor. Since the visitor was asked for his/her name, he/she will probably type in his/her name so name will probablly equal your user's name. That makes sense, right? You can prompt your user for anything, from his/her favorite color to his/her birthday. You can also change the name of the variable, by changing name to the variable you desire, but be warned. Once you change the variable for this command, you must change name to that same variable for every command in which name is found. To change the message, delete everything starting from Welcome and ending with the period after Name. Then, type in your question or statement asking for information. If you want to change the text that appears in the input field, delete the words Anonymous Web Cruiser and type the words you want to appear (Remember to keep the last parenthesis and semicolon in).
if(name == null) name = "Anonymous Web Cruiser";This part of the script tells the computer if name = null, then name = Anonymous Web Crusier. If these lines were not in the document source, every time the visitor clicks Cancel, name would be equal to null. If name = null, that's what the person is called! By puting the above fragment of source code, the threat of having your visitor being called null by pressing Cancel is eliminated. By typing if(name == null) name = "Anonymous Web Cruiser", you are assigning name to be equal to Anonymous Web Crusier if and only if the user clicks Cancel. If the person typed in null to be their name, the computer skips over the line, if(name == null) name = "Anonymous Web Cruiser"; because name is not equal to null! Really, it isn't, because null is not in quotation marks and null already has a value, the empty set, so name only has a value of null when your visitor clicks Cancel. If null is put in quotation marks, then the computer would only give the value of Anonymous Web Cruiser to name if the user typed in null because when it is in quotes, it is just like any word.
document.write("Welcome " + name + " To...");The precompiled function, document.write() outputs the text and/or does the HTML & JavaScript commands found in its parentheses. The above document.write() function outputs "Welcome (your visitor's name) To...". Name is not in quotes because it is a variable which stands for something so when the computer gets up to name, it outputs the character(s) that name stands for. If your visitor clicked Cancel, the text would read "Welcome Anonymous Web Cruiser To...".
// -- End hiding here -->This ends the commenting out for browsers unable to read JavaScript.
</script>This command tells the computer that the end of the JavaScript source has been reached. If you do not include this in your document source, the rest of your HTML document will be considered as JavaScript and the computer will not be able to comprehend the rest of the document. Also, the computer will give you Error Alerts.
JCL's HTML & JavaScript Tutorial