![]() |
|
Javascript Tutorial #2 Welcome to the second tutorial. Hope you got on alright with the first. No? Send me an email if there is something you don't understand and I'll try to help. Last time, I left you with a finished program and I ended by explaining about putting comments in your code. But wait, did you update your template file so that comments can be added more easily. No? This is what it should look like:- <html> One of the first things you will want to do is display messages. The simplest way is to use a message box or alert in JavaScript speak. Here's it's simplest form:- alert('Hello'); To test this, cut and paste and place it between the script tags. Your program should look something like this:- <html> Save as alert.HTM (or any name you like provide it ends in .htm) and double click on it in Windows (or Winnt) Explorer and see what it does. You must click OK to close the box. Pressiing F5 (see last tutorial) and will bring it back. Click OK and close the window in the usual way. Simple isn't it? You can display any word or words up to a limit of 255 characters provided it is written bewtween the quotes. Punctuation can also be displayed again provided it is between the quotes. However, unlike VBScript, the alert function is NOT capable of any extra functions. You cannot display a different icon nor can you specify a title for the box (another reason to like VBScript). From now on, I'll just be printing the code between the comments and the --> tag. I'll leave it up to you to paste the code between the tags and save it with a relevent name. Take alook at this. Now we can communicate with the user but very often, the user needs to give the program some kind of input. This is where the prompt function comes in. This is how it looks. var myInput; This code produces a box which the user can type into. It has a message telling the user what kind of input is required. Once the text has been entered, the user can either click OK (or press the Return key) to pass the value to the program and closes the box or click Cancel which simply closes the box. This example passes a value back but nothing is actually done with it. However, the following is a more useful program. var myInput; That completes this second tutorial. We'll get into something a little meatier next time. Please eMail me at: nickjc@nickjc.co.uk |
|