Web Client Programming

John "Scooter" Morris

May 18, 2009

Portions Copyright © 2005-06 Python Software Foundation.

Introduction

Web Programming

The Hypertext Transfer Protocol

HTTP Request Line

Headers

Body

HTTP Response

HTTP Response Codes

Questions on HTTP?

Client Programming

HTML Forms

Creating Forms

Form Fields

Form Fields: <select>

Form Fields: <button>

Form Fields: <textarea>

Form Fields: <input/>

A Simple Form

Sequence: Search type:

Programs: FROG (version 1.1) FROG (2.0 beta) Bayes-Hart

<html>
  <body>
    <form>
      <p>Sequence: <input type="text"/>
      Search type:
      <select>
        <option>Exact match</option>
        <option>Similarity match</option>
        <option>Sub-match</option>
      </select>
      </p>
      <p>Programs: 
      <input type="radio" name="alg">
        FROG (version 1.1)
      </input>
      <input type="radio" name="alg">
        FROG (2.0 beta)
      </input>
      <input type="radio" name="alg">
        Bayes-Hart
      </input>
      </p>
      <p>
        <input type="button" value="Submit Query"/>
        <input type="button" value="Reset"/>
      </p>
    </form>
  </body>
</html> 

Questions on Forms?

Javascript

Why Javascript?

Basic syntax

Getting your script to execute

Getting your script to execute (events)

Getting your script to execute (events)

  • As an action (event) handler:
    • HTML has a number of defined "events" that are expressed as attributes to HTML tags. The target of the event is a script:
      <p>This is a 
      <span style="color:green;border:thin blue solid;" onclick="alert('Hi!');">clickable</span> 
      word</p> 
      		
      Try me!

      This is a clickable word

    • Can also call your own function -- this is how form validation can be handled:
      <html>
      <body>
        <form>
          <script type="text/javascript">
            function validate()
            {
              var input = document.getElementById("number");
              var number = Number(input.value);
              if (number < 1 || number > 10 || isNaN(number)) {
                alert("Number must be between 1 and 10");
                input.value = "";
                return false;
              }
              return true;
            }
          </script>
          Enter a number between 1 and 10: 
             <input type="text" id="number" name="number" onchange="validate();"/>
        </form>
      </body>
      </html>
    • Try It!

HTML Events

TagsAttributeDescription
Window Events
bodyonloadScript to be run when a document loads
bodyonunloadScript to be run when a document unloads
Form Element Events
form elementsonchangeScript to be run when the element changes
form elementsonsubmitScript to be run when the form is submitted
form elementsonresetScript to be run when the form is reset
form elementsonselectScript to be run when the element is selected
form elementsonblurScript to be run when the element loses focus
form elementsonfocusScript to be run when the element gets focus
Keyboard Events
content elementsonkeydownWhat to do when key is pressed
content elementsonkeypressWhat to do when key is pressed and released
content elementsonkeyupWhat to do when key is released
Mouse Events
content elementsonclickWhat to do on a mouse click
content elementsondblclickWhat to do on a mouse double-click
content elementsonmousedownWhat to do when mouse button is pressed
content elementsonmousemoveWhat to do when mouse pointer moves
content elementsonmouseoutWhat to do when mouse pointer moves out of an element
content elementsonmouseoverWhat to do when mouse pointer moves over an element
content elementsonmouseupWhat to do when mouse button is released
Table 2: HTML Events

Javascript and the DOM

  • The HTML DOM is exposed through the document object
  • DOM methods provide access to the HTML elements:
    • document.getElementById(), document.getElementsByName(), document.getElementsByTagNameNS(), document.getElementsByClassName()
    • Note that only getElementById() is singular. All others will return an array of elements
    • Once you've found the element, you can look at it's properties:
      var x = element.innerHTML // the HTML contains within this element
      var x = element.style // the style information for this element
      var x = element.className // the element's class
      var x = element.attributes // the element's attributes
    • Elements also support the getElementBy methods. This allows you to walk the HTML tree, if you need to
  • DOM references:

Javascript and the DOM

  • DOM methods also provide a means to add to the HTML or to modify HTML elements:
    • document.createELementNS(), document.createAttribute(), and element.appendChild() provide the tools need to add to an HTML document
    • Many of the element properties are mutable. So, to change the class of an element, you could just change the className property.
  • Playing with classes (and the Gettysburg Address):
    classTest.html
    <html>
    <head>
            <title>Class change test</title>
            <link type="text/css" rel="stylesheet" href="classTest.css"/>
            <script type="text/javascript" src="classTest.js" ></script>
    </head>
    <body>
    <h1 class="header" id="header" onclick="changeClass(this);">Playing with classes</h1>
    
    <p class="plain" onclick="changeClass(this);">Four score and seven years ago ...</p>
    <p class="plain" onclick="changeClass(this);">Now we are engaged ...</p>
    <p class="plain" id="final" onclick="changeClass(this);">But, in a larger sense, we can not dedicate ...</p>
    <p class="hidden" id="citation">Gettysburg Address<br/>
    Abraham Lincoln<br/>
    November 19, 1863</p>
    
    </body>
    </html>

Javascript and the DOM (Example CSS)

classTest.css
h1.header {
        text-align: left;
        font-family: serif;
        color: black;
}

h1.title {
        text-align: center;
        font-family: sans-serif;
        color: green;
}

p.plain {
        margin-left: 0em;
        margin-right: 0em;
        font-style: normal;
}
p.quote {
        margin-left: 5em;
        margin-right: 5em;
        font-style: italic;
}
p.hidden {
        visibility: hidden;
}
p.visible {
        visibility: visible;
        float: right;
        color: blue;
        font-style: italic;
        margin-right: 5em;
}

Javascript and the DOM (Example JS)

classTest.js
// This function will change the class of the passed element
// Note that we deal with the header specially, and also
// handle the citation depending on the class of the final paragraph
function changeClass(element) {
        // Get the current class name of the element that called us
        var class = element.className;
        // Get the ID of the element that called us
        var id = element.id;

        // Did the header call us?
        if (id == "header") {
                // Yes, switch our class
                if (class == "header") {
                        element.className = "title";
                } else if (class == "title") {
                        element.className = "header";
                }
                return;
        }
        // Make sure it's a paragraph
        if (element.tagName == "P" || element.tagName == "p") {
                // Yes, switch it
                if (class == "plain") {
                        element.className = "quote";
                        // Is it the final paragraph?
                        if (id == "final")
                                // Yes, switch the citation
                                document.getElementById("citation").className = "visible";
                } else if (class == "quote") {
                        element.className = "plain";
                        // Is it the final paragraph?
                        if (id == "final")
                                // Yes, switch the citation
                                document.getElementById("citation").className = "hidden";
                }
        }
}

Other Javascript Objects

  • JavaScripts supports the following built-in objects:
    • Types: Array, Boolean, Date, Number, String
    • Math
    • RegExp
    • Top-level functions: decodeURI(), decodeURIComponent(), encodeURI(), encodeURIComponent(), escape(), eval(), isFinite(), isNaN(), Number(), parseFloat(), parseInt(), String(), and unescape()
    • Browser interface objects: Window, Navigator, Screen, History, and Location
    • DOM objects, some of which are: Document, Form, Option, Select, and Style
    • Various HTML elements are exposed as objects, but I think it's better to work through the DOM methods directly

Debugging client code

  • Tools:
    • Firefox
      • Most standard-compliant browser
      • Firefox "Tools→Error Console" will really help!
    • Firefox add-ins: Web Developer, JavaScript Debugger
      • Firebug is also excellent, but not (yet) available for Firefox 3.0
  • Technique:
    • Separate CSS and JS from HTML file, unless you have < 10-15 lines
    • Start with HTML & CSS - no JS
    • Before coding, search the web for examples
    • Incrementally add and debug JS
    • alert() is a useful, quick debugging aid

Client-side Programming

  • Questions?
  • Overwhelmed?

Assignment

  • Before class tomorrow:
    1. As discussed on Friday, put together the major use cases for your web site
    2. Choose one use case to prototype, and put together a low-fidelity prototype (paper, whiteboard, etc.)
    3. Develop a non-functional HTML prototype of your site:
      • Put your HTML in /home/socr/b/bmi280?/html
      • Put your Javascript in /home/socr/b/bmi280?/html/js
      • Put your CSS style sheets in /home/socr/b/bmi280?/html/css
    4. Test your site by going to: http://socrates2.cgl.ucsf.edu/bmi280?/file.html
    5. Where the "?" should be replaced by "a" or "b" as is appropriate for your team
  • During class tomorrow:
    1. Add Javascript to your prototype to validate input, expand fields, and in general present the appropriate options to the user
  • Questions?