Javascript and the DOM

Dreamweaver let's you create many "behaviors" that use javascript, without having to know exactly what's going on.

But with just a little knowledge of what's going on, you can take advantage of the many repositories on the Web of useful, free-to-use javascripts, and write some simple ones for your own projects.

Readings

castro6.gif Chapter 12



Two ways of running a javascript program

Run a script in the sequence of rendering the page.

Here's a sneaky way to display your e-mail address:

<p>Here's a sneaky way to write your e-mail address:
<b style='color: #b33'>
<script type="text/javascript">
document.write("paulmr"+"@goshen.edu");
</script>

</b></p>

Or, this is can be re-written as a function declaration in the head section of a document:

<head>

<script type="text/javascript">
<!--
  function showGCaddress(username){
     document.write( username + '@goshen.edu' );
  }
// End -->
</script>

</head>
<body onLoad="[initialize something]">
 

And then, at the point you wish to show your address you do this:

<script type="text/javascript">
showGCaddress("paulmr");
</script>

Some user action triggers a script

Where is that new world? [javascript image swap example]

East?     or     West?

Code for the above:
    <img src="qmark.jpg" width="100" height="157" id="whither" />

    <p align=center>
    <a href=""
    onmouseover="document.getElementById('whither').src='colonr.jpg'"
    >East?</a>
    or
    <a href=""
    onmouseout="document.getElementById('whither').src='colonl.jpg'"
    >West?</a>
    </p>

Javascript examples

Events

Things that can happen as the user interacts with the page are called events, and particular events can trigger actions specified by javascript commands. A non-exhaustive list of events...


The mouse enters the box associated with some tag.
onMouseOver
The mouse leaves the box.
onMouseOut
The mouse is pressed and released.
onClick
The 'submit' button is pressed (applies to the <FORM ... > tag).
onSubmit
One item from a drop down menu is selected (applies to the <SELECT> tag)
onChange
A page loads (applies to the <BODY> tag)
onLoad
an element is selected (de-selected)
onFocus (onBlur)


the Document Object Model (DOM)

The DOM is a way of naming (and changing!):

Changing tag attributes

Try each of these by moving your mouse over top ('onMouseOver') the link...

<a href='' onMouseOver="document.getElementById('colon').hspace='35'">do it</a>: do it undo it


...ById('colon').width='157': do it undo it
...ById('colon').align='left': do it undo it
...ById('colon').border='3': do it undo it

The image at the right has id='colon', and here's a bit of text that wraps beside it.


Changing tag styles

document.getElementById('sp').style.color='#5b5': do it undo it
...ById('sp').style.fontSize='2.5em': do it undo it
...ById('sp').style.fontFamily='Times,serif': do it undo it
...ById('sp').style.border='1px dashed #933': do it undo it
...ById('sp').style.display='none': do it undo it

This here text is in a paragraph marked with id='sp' ("sample paragraph"). So let's see if we can change its style.

Or, a couple all at once:

aStyle=document.getElementById('sp').style;
aStyle.marginLeft='20px';
aStyle.textIndent='-20px'
do it undo it

Changing the text inside a tagset

do it

This here is the contents of a div marked with id='sd'. Should all go well...

Here's an example that does a number of things: changing the position and sometimes the content of apLayers on a page: Scaredy-Fish... (Works in FF, IE 6, 7).

See also

Taking over 'onClick'

<a href="javascript;" onClick="document.getElementById('whither').src='colonr.jpg'; return false">Point right</a>

How Javascript is organized

One or more javascript commands can be executed at the place in the web page where they occur, between <script>...</script> tags like this:

<script type="text/javascript">
<!--
document.write('My e-mail address is : ');
document.write('<a href="mailto:paulm'+'r@goshen.edu">paulm'+'r@goshen.edu</a>');
//-->
</script>

If an event triggers more than just one or two javascript commands, the commands are typically packaged up in a function declaration -- sort of a little computer program -- which is usually placed in the <head>..</head> section of a web page.

<head>

<script type="text/javascript">
<!--
function topWindow(){
popup = window.open("jscript/newwin.html","","height=350,width=660,scrollbars=yes");
}
//-->
</script>
</head>
<body>

<form>
<input type ="button" value="Open a window" onClick="topWindow()">
</form>

Or, (just like with CSS rules) javascript can be put in an external file that you link to like this:

<script type="text/javascript" src="prototype.js"></script>

To transfer javascript functionality from one page to another you usually have to copy the triggers and the functions. Sometimes a third thing that must be copied is a function call in the body tag, e.g. <body onLoad="something()">, a piece that initializes things as the page loads.

HW: Use a javascript library

Lightbox is an open-source javascript library that you can use to display images in an animated fashion.

Make a copy of your digital camera lab in the same folder as the original. Make sure that your thumbnails are linking to the larger images directly (not to a *page* with the larger image embedded). (I'll use this lighter-weight page, lb.html, as an example.)

Follow the "How to use" instructions on the page above, but instead of using the locations that they suggest for the javascript and css files, use the location of the ones already installed on the GC webserver:

/js/prototype.js
/js/scriptaculous.js?load=effects
/js/lightbox.js

/g/lightbox.css

Huh? No mouse events to re-program? Here lightbox is replacing one of the "normal" actions on our page (clicking a link to navigate somewhere else) with its own scripted action.