execute js function

M

Mariano

I'm going to explain better.
I have a js file that will call a java servlet, this servlet will gave
me a response back. This response is in XML format between tag <msg>.
This response should be a call to JS function, this is the response
example:

<msg>functionJS("param1")</msg>

Successively in the js i will read the msg node (from the servlet)
with an XPATH query and then I would use functionJS, but all that i
can do is print litterally the <msg> content.

This is the JS thath call servlet and make XPATH:

function servlet_call() {
var doc = loadXMLDoc("servlet?id=0"); // load xml from servlet
response
var msg = myxPath(doc, "/msg"); // obtain the content
between <msg>

var target = document.getElementById("ajaxDiv"); // ajax
target.innerHTML=msg; //
ajax
}

As you can imagine, i don't want print the msg content, but, execute
it. Some suggest???
bye
 
V

VK

I'm going to explain better.
I have a js file that will call a java servlet, this servlet will gave
me a response back. This response is in XML format between tag <msg>.
This response should be a call to JS function, this is the response
example:

<msg>functionJS("param1")</msg>

Successively in the js i will read the msg node (from the servlet)
with an XPATH query and then I would use functionJS, but all that i
can do is print litterally the <msg> content.

This is the JS thath call servlet and make XPATH:

function servlet_call() {
var doc = loadXMLDoc("servlet?id=0"); // load xml from servlet
response
var msg = myxPath(doc, "/msg"); // obtain the content
between <msg>

var target = document.getElementById("ajaxDiv"); // ajax
target.innerHTML=msg; //
ajax

}

As you can imagine, i don't want print the msg content, but, execute
it. Some suggest???


var msg = myxPath(doc, "/msg");
try {
eval(msg);
}
catch(e) {
// invalid Javascript
// or runtime error,
// yours to handle
}

But really if you are using RMI-like pattern then your should use JSON
instead of XML. XML here is just not the right tool for the job.
 
J

Joost Diepenmaat

Randy Webb said:
Joost Diepenmaat said the following on 1/31/2008 2:26 PM:
you want eval("your downloaded javascript code here");

No, you don't. There is a better alternative and just as modern
browser friendly without all the headaches of eval.
function executeJSCode(codeToExecute){
if (document &&
document.createElement &&
document.appendChild &&
document.getElementsByTagName)
{
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = codeToExecute;
document.getElementsByTagName('head')[0].appendChild(newScript);
}
}

Look ma, no eval!

How is that better than eval? As far as I can see it's probably slower
(not that eval is fast or anything) and a lot more cumbersome. All of
the usual headaches of eval are still there - you just moved the call to
eval() to the DOM.

Joost.
 
T

Thomas 'PointedEars' Lahn

You are jumping to conclusions.
[using DOM Level 2 Core to append a `script' element]

This approach is based on the assumption that the addition of a script
element to the document executes the referred code, if and only if that code
can be referred to.
How is that better than eval?

It isn't.
As far as I can see it's probably slower (not that eval is fast or
anything) and a lot more cumbersome. All of the usual headaches of eval
are still there - you just moved the call to eval() to the DOM.

eval() code at least can be debugged. Something better than eval() could be:

(new Function(code)).call(this);


PointedEars
 
H

Henry

I've just been looking at JSON (yeah, I'm behind the times) and
the easy method to instantiate an object is eval. Is there a
scope problem here?

JSON is designed such that scope cannot be an issue (you cannot use an
Identifier, property accessor or FunctionExpressions in valid JSON).
I notice that this is done this way:

eval('(' + text + ')');

instead of eval(text)

I'm missing a bit of understanding about the syntax.

It is a question of the difference between an Expression and a
Statement. Javascript has block statements; a (possibly empty) list of
statements surrounded by braces. Statements can contain Expressions
and Expressions can contain (sub)Expressions, but an Expression can
never contain a Statement (with the exception of the bodies of
functions in FunctionExpressions).

The - eval - function executes the code in its string arguments as
javascript programs and so will attempt to interpret what it sees as a
mixture of function declarations and statements.

JSON strings are strings containing the source code of object and
array literals, so the outermost two characters at the ends of that
string will either be a pair of braces or a pair of square brackets.
Square brackets would not be a problem but braces would.

Suppose the JSON string were - '{"somthing":"XXX"}' -, an attempt to
interpret that as a statement could not see an ExpressionStatement as
ExpressionStatements may not start with an opening brace (ECMA 262 3rd
Ed. Section 12.4), which is a syntax rule that acts to make
interpretation non-ambiguous. Instead the only type of statement that
can commence with an opening brace is a Block statement, and so that
must be how - eval - interprets the string, and trying to interpret
the string as a block statement will result in a syntax error.

When the JSON string is surrounded by parenthesise it can only be
interpreted as an ExpressionStatement; a grouping expression
consisting of parenthesise surrounding another expression. So the JSON
string will then only be interpretable as an Expression and in an
expression context an initial opening brace can only be the beginning
of an object literal. Thus the surrounding parenthesises result in the
JSON object literal being interpreted as the object literal that it is
intended to represent.
 
L

Lasse Reichstein Nielsen

Jeff said:
I've just been looking at JSON (yeah, I'm behind the times) and the
easy method to instantiate an object is eval. Is there a scope problem
here?

Hopefully not. A JSON value does not contain free variables, nor
fuctions that can capture scope, so it is completely independent of
which scope it is evaluated in.
I notice that this is done this way:

eval('(' + text + ')');

instead of eval(text)

I'm missing a bit of understanding about the syntax.

A JSON object literal is, e.g., "{ foo: 42 }".
If you pass that as a an argument to eval, it will be interpreted
as a statement, in particular a statement block containing a labeled
constant expression statement.
To ensure that it is evaluated as an expression, it is wrapped in
parentheses.

/L
 
A

AKS

...nor
fuctions that can capture scope, so it is completely independent of
which scope it is evaluated in.

Not quite so. In JavaScript (SpiderMonkey):

eval('var x = "global";');

(function () {
eval('var x = 1;');
(function () {
eval('var x = 2;');
(function () {
eval('var x = 3;');
window.eval('var f = ' + f.toString()); // inherits Scope
of outer functions
}());
}());
}());

function f() {
while (typeof x != 'undefined') {
alert(x);
delete x;
};
};

f(); // -> 'global', 3, 2, 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top