[Javascript] Replace/modify javascript functions at runtime?
Håkan Magnusson
hakan at backbase.com
Mon Oct 27 05:25:45 CST 2003
Note, this only has to work in Mozilla as of now. Let me illustrate an
example.
------ text.js:
function Plus(iArg1, iArg2) {
return (sArg1 + iArg2);
}
/------ text.js:
Now if I check 'window.Plus' i find the string (representation?)
"function Plus(iArg1, iArg2) { return (sArg1 + iArg2);}". All good and
well. Now I want to add one line of code in this function, at runtime.
sNewFunc = window.Plus.toString().replace('{', '{ MyFunction();');
and sNewFunc now displays:
"function Plus(iArg1, iArg2) { MyFunction(); return (sArg1 + iArg2);}"
Then I'm doing the following (I think this is where I do it wrong):
window.Plus = sNewFunc;
This "works", in that it doesn't generate any errors. When I check
window.Plus again, my "new" function is returned. When I run the
window.Plus function, however, it seems that the browser is still using
the "old" function.
If what I'm trying to do is possible, it would be VERY cool, and I'm not
afraid of complex solutions, as long as they are managable. I had an
idea of creating a whole new "container" (documentFragment?) for my
"rewritten" functions, but err...
Håkan
More information about the Javascript
mailing list