In this example I am using the document.write which means write to the document (the document being the available area on the screen). I am writing what is enclosed in () and since what I am writing is a literal, I put in in quotes. Actually what happens here is that the message in quotes is handled as if it was written in HTML.
The alert() puts up a box. The message in the box is the message I enclose in the Parenthesis. Again, it is a literal so I enclose it in quotes.
The next slides will show the output.
When I am ready to save my code, I save it with an html extension. Note that I am writing my code in notepad.
To open the file that I saved as hello.html, I first go to the browser and select file open. In explorer I will see the box shown. I select browse and then find my file and select it. Then I click open. I will see the results in the browser window.
When I am working with html, I keep both the browser (or usually a couple of browsers open) and I keep notepad open so I can switch back and forth between my code in notepad and the results in the browser window.
This shows the output when hello.html is opened in the explorer browser.
If you know HTML, you can make the Hello Write that appears on the screen a different size and a different color etc.
I have now written the code on this slide and saved it as multiply.html.
In the calculation: ans = firstnum * secondnum, the = is an assignment sign. It is assigning the answer to the calculation to the variable ans. The reason that this is called an assignment sign is that code like the following is legitimate: ans = ans + 1. In fact we know that ans can never be equal to ans + 1, but we can take ans and add 1 to it and put it back into ans. That is called assigning.
This shows entering the number 5 as firstnum and then the number 7 as secondnum. Finally the results of the multiply calculation is shown on the screen using document.write(ans);
The change is putting in a literal to identify the answer. In this case, the document.write contains both a literal and a variable. They are separated with a comma.
This shows the results of using a literal to identify the answer. The code is named multiplyans.html.
In this slide, I am executing mathans.html. I have entered 15 and then 3 and then *. The result of multiplying 15 by 3 is shown as 45.
This time I again entered 15 and 3, but I entered the / to divide. The results of dividing 15 by 3 is shown as 5.
The <BR> is html for break or skip a line. If I enclose the html tag inside my parenthesis and the quotation marks, it says to treat the data as html, which means it displays the literal and then skips a line.
This shows another example of an if statement. In this example I am testing the application name to see if it is Netscape. To do this I test navigator.appName. Note that I use the two equal signs together since I am testing a condition for equality. The structure of the if calls for the commands to be executed if the condition is true to be enclosed in curly brackets and the commands to be executed if the condition is false to be enclosed in curly brackets. Each command within the curly brackets ends in a semi-colon.
When the navigator.appName is not Netscape, then the else is taken. Again remember that each command ends with a semi-colon and all of the commands for the else are enclosed in curly brackets. One other comment the literal followed by <BR> is all in quotes and is handled by html by displaying the literal and moving to the next line. Note that I said probably Explorer because it could be another browser, Opera for example.
Concatenation means putting two things together as one. In the first example in this code, I am concatenating a literal with the contents of ct and then concatenating that with the <br> in quotes that will tell the code to skip a line. Remember that the things inside the parenthesis are processed as html so I see the literal, the contents of ct and then I skip a line. Note also that I put a space after the word number so that there will be a space between the literal This is a number and the variable ct.
This show the output from the program whileloop.html. Note that when prompted for how many times I wanted to write the line, I entered 6. The line then got written 6 times. The message, This is the end!!! is outside the while loop and gets written when the loop is complete.
Again remember that literals are enclosed in quotes and variables are not. In this example, I have concatenated several variables and literals together to show the date.
Here I am taking in month, day and year. The results of the document.write are shown here.