Using Javascript

Chapter 13, some of 14.

  1. [ Simple OnClick Source ]
  2. [ Colored Dots Source ]
  3. Inserting into browsers.
    1. Usually works fine: <script></script>.
    2. More formal: <script type="text/javascript"></script>.
    3. Obsolete: <script language="javascript"></script>.
    4. Can go anywhere, but often wise to place into the head.
    5. When javascript is not implemented or disabled:
      <noscript> <p>I'm sorry, your browser doesn't support Javascript.</p> </noscript>
      1. Can contain any HTML.
      2. Ignored by a browser which supports scripting.
    6. For an ancient browser which doesn't understand <script> at all.
      <script type="text/javascript"><!-- alert("Hello, world!"); // --></script>
      1. The browser that understands <script></script> sends the contents to a javascript interpreter.
      2. One that doesn't would display the script, since the rule to to ignore tags you don't understand.
      3. But it sees the HTML comment <!-- --> which suppresses the display.
      4. Javascript seems to tolerate the <!-- in the code. Not sure why.
    7. Remote files of Javascript:
      <script type="text/javascript" src="somedocument.js"></script>
      The document is a URL, so can be loaded from anywhere.
  4. Error messages appear on the Javascript console.
    <button onClick="var bill = 4 8 foo">Push Me</button>
  5. Debugging output
    1. console.log("message"); will appear in the console.
      <button onClick="console.log('Console Message')">Push Me</button>
    2. alert("message"); will pop up.
      <button onClick="alert('You Are Alerted')">Push Me</button>
    3. Write a message into an existing div. More work.
      <button onClick="document.getElementById('logbox').innerHTML += 'Log Message<br>'">Push Me</button>
    4. document.write("message");
      1. Generally avoid.
      2. Works okay when when run in-line as page is loading.
      3. If called after page is loaded (from a function, say), will replace the entire document.
  6. User Interaction
    1. Alert on load. (Booooo). [ Popups Source ]
    2. Alert on a button (as above).
    3. Popup window. [ Create Window Source ]
  7. Basic Syntax.
    1. Similar to PHP, reflecting common ancestry.
    2. Same comments. // /* */
    3. Semicolons generally optional.
  8. Expressions.
    1. Variables
      1. Can include $ as a letter.
      2. Case sensitive.
      3. Does not need $ to set off variables like PHP.
    2. Strings.
      1. Either " or '
      2. Equivalent, and may contain each other.
      3. No interpolation.
      4. No block quotes.
    3. Numerics
    4. Arrays: name = [ 'this', 'that', 'the other' ];
    5. Associative Arrays: { "cat" : "meow", "cow" : "moo", "bird" : "chirp" }
    6. Operators: usual suspects. +, -, *, /, %, ++, --, =, ==, !=, <, >, <=, >=, ===, !==, &&, ||, !
    7. Also += and the other update operators.
    8. String concat is + instead of ..
  9. Dynamic typing.
  10. Functions.
    function fred(x,y,z) { return x*y+z; }
  11. Local v. global
    1. var declares a variable local to its function.
    2. Just use the name to create a variable global to the page.
  12. if, while, switch: Familiar syntax.