Eval in Javascript

This page is probably best viewed in conjunction with its source; it's much easier to see what's going on.

This demostrates the use of eval() to define new functions in Javascript. It'll probably require a reasonably up-to-date browser. The first button calls a function which uses a string to define the function p(t); the second button again attempts to call p, which should have been defined after clicking "Test eval".

As you can see, the definition of the funciton p() is nested inside the event handler for the first button.

However, there is also some similar imperative code to attempt to define q(t) at the global level; the third button attempts to call that.

This trick can be used to get javascript pages down to _very_ small footprints, by expanding the entire javascript program from a compressed string and using eval on it :-)