Script Deconstruction

  • We start with the <SCRIPT LANGUAGE="javascript"> statement.
  • The variable "name" is created. The person's name will come from a prompt.
  • We need a date so the variable "d" is created for the date.
  • We know that the year, month, and date will be d.getSomething, so we create variable names for each of those items, "y", "m", and "d".
  • See the "+ 1900" after the year? The logic is that if only the last two numbers of the year are returned, then adding 1900 to it would give the correct year in four digits.
  • See that "+ 1" at the end of the "m" variable? That's a trick that sets the year to the correct count. It's a good idea to use it when you ask for the month.
  • The fully written date itself is also given a variable "t".
  • I created the TITLE command by creating three "document.write" commands. I did it because it's easiest to separate the HTML commands from the JavaScript code.
  • The first is the beginning TITLE command.
  • The second is the statement that creates the text line. Notice the text that is to be written in double quotes, separated from the commands that return data by plus signs. Notice the text includes spaces for continuity.
  • The third "document.write" then contains the end TITLE command.
  • The </SCRIPT> wraps it up.
  • One other fairly important item to remember is that this script must go above the BODY command in the HTML document, because that's where the TITLE goes.

Oh, and in case you're wondering. Your name was placed in the text using this command:

<SCRIPT>document.write("" +name+ "")</SCRIPT>

Close this window to return to JavaScript Primer #1