Basic HTML version of Foils prepared 23 June 1997

Foil 35 The Keyword this

From JavaScript Tutorial for Certificate Course UC Web applications Certificate -- Summer 97. by Geoffrey C. Fox, Tom Scavo


The this keyword is very useful but confusing since it's not always clear what it refers to
  • Not always clear when this is needed
Here is an example that sets the URL for a link:
function setURL(obj) { // Put in <head> container
obj.target = "Desiredframe"; // set frame
obj.href = "http://www.npac.syr.edu"; // set URL
}
In the body of the HTML document:
<a href="" onClick="setURL(this)" onMouseOver="window.status='URL set dynamically in method setURL'; return true">Click Here for Dynamic URL</a>
Here this refers to a link object created by <a>..</a>
window.status is line at bottom, which usually records URL
Note setURL overrides href specified in <a> tag



Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998