References Used


History


Uses and Limitations


A Brief Example

Javascript was developed to enable interaction with the browser so let's start with a quick example of how it enables such interaction. The example snippet below changes the text "Hello World" to "Hello Brad!" when the button is pressed and changes the color of the text from black to green:

<head>
  <script>
    function handleClick() {
      console.log("handleClick");
      let announcement = document.getElementById("announcement");
      announcement.textContent = "Hello Brad!";
      announcement.style.color = "green";
    }
  </script>
</head>

<p id="announcement">Hello World</p>
<button type="input" onclick="handleClick()">Press Me!</button>
  

The javascript code is embedded in a <script> tag in the head section. When the button is clicked, the "onclick" event calls the handleClick function. This function does three things:

  1. it prints a message to the browser console.
  2. it retrieves the html element displaying the text "Hello World" and changes it to "Hello Brad!".
  3. it changes the color of the text to green.
HTML elements are stored in a data structure called the Document Object Model (DOM) and can be retrieved and acted on by various Javascript functions. Additionally each html element has a style object containing CSS key/value pairs that can be modified by various Javascript functions. Over the next couple of weeks we will be discussing how Javascript can interact with the DOM to affect the display of a web page. However, we must first start with the basics of the Javascript language, which is what is done in this set of notes.


JavaScript Terminology


Embedding JavaScript in an HTML Page


Debugging JavaScript


Variables, Operators, and Expressions


Comments


Input and Output

  • Input can be obtained in one of two ways:

    1. via form elements (to be discussed later)
    2. via the prompt function

      1. the prompt function pops up a dialog box with a text box and allows the user to enter a string of input
      2. the prompt function returns the string as its return value
      3. syntax: prompt("prompt message", default value)
      4. example: prompt("please enter sales tax rate", ".05");
      5. it is ok to provide an empty string, '', as the default value, or to simply omit the default value, in which case it will default to the empty string
      6. it is ok to provide any evaluable expression as the default value--the resulting value will be converted to a string
      7. if the user presses the "Cancel" button then prompt returns null
  • Output can be provided in a number of ways

    1. document.write(string): document is a pre-defined object and write is a method that writes a string. You use the write method to dynamically create content on a web-page as it it being loaded.
      1. You can use the string concatenation operator, +, to concatenate several string fragments.

      2. You can write html markup in order to obtain formatted output.

      3. write destroys the content that was previously displayed on the web page so do not use it after the html page has loaded. Once the html page has loaded, you should modify the page by adding or deleting nodes from the dom tree (talked about in the AJAX notes).

      4. document.writeln will write a string with a newline attached to it but it is of limited utility because 1) the newline does not show up in the html output--<br> would be needed to do that, and 2) the source view will show the script, not the dynamically generated html so the string will not show up in the source view.

    2. console.log(string): writes a debugging message to the browser console. This message will not appear in the web-page.
    3. alert(string): alert will pop up a dialog box with the given string value. Execution of the script will be suspended until the user presses the ok button.

    4. alter the html dom: You can use javascript to modify, add, or delete nodes from the html dom, and this will adjust the contents of the page. We will discuss the html dom in later notes.

    Ifs and Loops


    Arrays


    Functions


    Objects