Discussion:
Compiling <script> src into a function
Mohan
2009-10-28 22:40:38 UTC
Permalink
Hi All,

I would like to know if there is a mechanism to compile the script
code from the "src" attribute of the <script> tag into a function that
can be later invoked from my JS code. For eg:

<script src="some.url">
Now this if the source script compiles to a function say "foo", then I
would like to execute the script by calling foo();

I have tried to make changes to the nsJSContext::EvaluateString()
method in nsJSEnvironment.cpp to compile it to a function using
JS_CompileUCFunction. When I later use the compiled function in a JS
code, it does not work. I then used JS_CallFunction() in
nsJSEnvironment.cpp on my compiled function and the script executed,
but is it possible to achieve the above functionality from within the
JS code.

I am sure I am missing something. Could anyone suggest some pointers
and/or modifications that may be required?

Regards,
mohan
Igor Tandetnik
2009-10-28 22:52:30 UTC
Permalink
Post by Mohan
I would like to know if there is a mechanism to compile the script
code from the "src" attribute of the <script> tag into a function that
<script src="some.url">
Now this if the source script compiles to a function say "foo", then I
would like to execute the script by calling foo();
I suppose you could download the text from some.url using XMLHttpRequest, then surround it with "function myfunc() {" and "}", and finally eval() the whole thing.

Igor Tandetnik

Continue reading on narkive:
Loading...