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'");
|