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


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

in Table To:


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