Question:
javascript: How do I take a string variable and evaluate it as an expression?
Evan R
2008-04-23 10:25:23 UTC
I am replacing an old MS/EI HTC implementation with something more standards based, but want to disrupt the existing code as little as possible. The existing app has a set of UI controls that are marked up as follows:

onselect= "document.MessageForm.fromUserID.value = event.selected; this.changeInnerHTML(event.selectedDisplay);"
...other attributes that describe the pull down list...
/>

The important part of this story is the 'onselect' attribute that I will need to evaluate.

I am successfully parsing the app pages to find these 'itemPickers' and extracting the attributes I need to reconstruct the control without using the HTC file, but in order to make the control actually work, I need to be able to access that 'onselect' attribute as an expression and not just a string.
Three answers:
Random Malefactor
2008-04-29 23:53:33 UTC
Yes eval is what you need to call, to execute what essentially amounts to dynamically generated script code. The eval() function executes its first argument in the script engine, and (in the case of javascript) returns the value of the last statement in the code. e.g.,



eval("i = 1; i += 1;");

eval("i = 1; 2;");



would each return 2. VBS plays by somewhat more restrictive rules, if you want to eval a chunk of VBS code, it must be a single statement, and it must explicitly resolve to a value. (The resulting value of an assignment does not count.) It's actually somewhat difficult to use eval with VBS code, Exec tends to be more forgiving syntactically, but it does not return a value.



Note that code running in eval or Exec has access to variables declared at the same scope at which either are called, so be careful not to overwrite any data used by the rest of your static code.



Good Luck!



Edit:



The scope in which your dynamic script runs is identical to that at which eval() is called. If it's called from within a function, it has access to variables declared in that function's scope, plus anything declared at document scope. (Don't forget that implicitly declared variables in javascript exist at document scope, even if first referenced inside of a function.)



Note that the string expression passed to eval should look just like regular code when resolved, so this (identifiers shortened to avoid y!a butchery):



eval("alert('obj.firstAtt')");



will display the literal string 'obj.firstAtt' in an alert, because... um... well because that's what it's coded to do. :-) Skip the single quotes if you want the value of that attribute.
anonymous
2008-04-23 19:09:16 UTC
The way that works in javascript is:



var html = '';

html += '

html += var 'onselect="'

html += ...



a function rewrites String var html with a call

then

document.write(html);
Greg S
2008-04-28 12:09:28 UTC
Sorry if this is a silly answer :-S, but can you not just convert your string into a regular expression:



var example=("example");

var the_answer=RegExp("example");

var the_answer2=RegExp(example);



sorry if this is no use :-/ don't quite get the question lol!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...