Compiling <script> src into a function

View: New views
2 Messages — Rating Filter:   Alert me  

Compiling <script> src into a function

by mohan.dhawan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine

Re: Compiling <script> src into a function

by Igor Tandetnik :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Mohan <mohan.dhawan@...> wrote:
> 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 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

_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine