Well, I have a lot of variables in javascript that I need get the values(that I getting from other page).
What s the best way to do this?
I m using the Microsoft.Jscript
class and your methods for it work.
I written the following code:
static Dictionary<string, string> ParseVariables(string code)
{
string[] variables = code.Split( ; );
Dictionary<string, string> variablesValues = new Dictionary<string, string>();
for (int i = 0, len = variables.Length - 1; i < len; i++)
{
string vvar = variables[i];
string varName = Regex.Replace(vvar.Split( = )[0], @"^s*vars*", string.Empty).Trim();
var compiler = Compile(vvar);
string varValue = compiler.ToString();
variablesValues.Add(varName, varValue);
}
return variablesValues;
}
static object Compile(string JSource)
{
return Microsoft.JScript.Eval.JScriptEvaluate(JSource, Microsoft.JScript.Vsa.VsaEngine.CreateEngine());
}
在某些案件中,这种做法是罚款的:
var jscript = "var x = foo ; var y = baa ;";
var output = ParseVariables(jscript);
var val = output["y"]; //baa
var val2 = output["x"]; //foo
但情况如下:
var jscript = "var x = {}; x[ foo ] = baa ; x[ baa ] = test ;";
var output = ParseVariables(jscript);
var val = output["x[ foo ]"]; //not works
我如何这样做? 任何帮助都值得赞赏。 感谢!