Basic HTML version of Foils prepared 23 June 1997

Foil 59 The Function Object and Event Handlers

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


One can define a string of JavaScript as a function object:
target = new Function( [arg1,...,argn,] functionBody )
where the arguments arg1,...,argn are optional
target is either a variable, a property of an existing object, or an event handler such as window.onerror
var setBGcolor =
new Function("document.bgColor='antiquewhite'");
You can now execute setBGcolor() but note that the function can be changed at any time. For example,
setBGcolor = new Function("document.bgColor='pink'");
One can define event handlers dynamically, but they MUST be lowercase and they MUST have no arguments:
  • window.onfocus = setBGcolor;
Like eval, the function body may be constructed dynamically:
  • var myProp = 'bgcolor';
  • Function("document." + myProp + "='antiquewhite'");



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