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