GSP Lab 2015 : Modifying Pages with Javascript

Modifying Pages with Javascript

Markup languages are static. Algorithms are dynamic. We consider the ways in which we might write algortihms relating to Web pages.

Getting Started

Making Pages Interactive

It is clearly possible to write instructions that manipulate an existing Web page. (In fact, you can do that to any Web page you find, as long as you can identify the parts.) However, as page authors and designers, we should really be providing the interactivity, rather than hoping someone will type something. That is, we need to put instructions for interacting with the page inside the code for the page.

Traditionally, we make pages interactive with "handlers". Handlers take the form "When the user does this to this element, do this." The way we write handlers can be a bit odd. We'll use an example, and hope you can figure out how to generalize it.

    document.getElementById("ubw101").onmouseenter = function() {
       document.getElementById("ubw101").innerHTML = "Underwater Basket Weaving";
       document.getElementById("ubw101").style["color"] = "blue";
    }

These instruction explain what the browser should do when someone moves the mouse over the element with id "ubw101".

As you may have noted, once we've changed the text, it doesn't change back. We might want to make it change back after we move away. We'll use a similar technique, replacing onmouseenter with onmouseleave.

    document.getElementById("ubw101").onmouseleave = function() {
       document.getElementById("ubw101").innerHTML = "Mentor, UBW 101";
       document.getElementById("ubw101").style["color"] = "black";
    }

Earlier in the lab, we made the résumé a bit better by fixing formatting. Now we'll make it a bit more "interesting" (better for some, worse for others) by making some of the headings change. Consider the following instructions.

    document.getElementById("education").onmouseenter = function() {
      document.getElementById("education").innerHTML = "Things I Learned";
    }
    document.getElementById("education").onmouseleave = function() {
      document.getElementById("education").innerHTML = "Education";
    }

    document.getElementById("experience").onmouseenter = function() {
      document.getElementById("experience").innerHTML = "Things I Did For Money";
    }
    document.getElementById("experience").onmouseleave = function() {
      document.getElementById("experience").innerHTML = "Experience";
    }

    document.getElementById("volunteer").onmouseenter = function() {
      document.getElementById("volunteer").innerHTML = "Things I Did For Love";
    }
    document.getElementById("volunteer").onmouseleave = function() {
      document.getElementById("volunteer").innerHTML = "Volunteer Work";
    }

All of these examples make an element change itself. But what if you want to change another element? Amazingly, the code isn't all that much different. Consider the following instructions.

   document.getElementById("stupid").onmouseenter = function() {
     document.getElementById("interests").style["text-align"] = "left";
     document.getElementById("interests").innerHTML = "AI";
   }
   document.getElementById("stupid").onmouseleave = function() {
     document.getElementById("interests").style["text-align"] = "right";
     document.getElementById("interests").innerHTML = "Additional Interests";
   }

You've now seen a variety of similar changes. Let's go beyond them.