4

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*var\s*", 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());
        }

This works fine to some cases,like:

var jscript = "var x = 'foo'; var y = 'baa';"; 
var output = ParseVariables(jscript); 
var val = output["y"]; //baa 
var val2 = output["x"]; //foo

but to cases like this:

var jscript = "var x = {}; x['foo'] = 'baa'; x['baa'] = 'test';"; 
var output = ParseVariables(jscript); 
var val = output["x['foo']"]; //not works 

How I do this? Any help is appreciated! Thanks!

3
  • It's very unclear what you're asking about. Is this an ASP.NET page? Is the JavaScript you're talking about running in the browser? Commented Oct 10, 2011 at 2:25
  • @Jonh: Is this an console application. The javascript code I getting from other page as an string Commented Oct 10, 2011 at 2:41
  • This is a very unusual environment. I strongly recommend you describe it by editing your question. Commented Oct 10, 2011 at 2:42

2 Answers 2

2

Since your approach is to split the JScript source code in chunks separated by semicolon (;), only the part between var and ; will be compiled using your Compile method.

If you change your JScript source code into var x = { "foo": "baa", "baa": "test" };, the Compile method will work properly, and it will return a ScriptObject object.

But then, there is another error - you are using ToString before you insert the value into the resulting Dictionary.

Try this out to get started in a better direction:

Change the Compile method into returning ScriptObject, like this:

static ScriptObject Compile(string JSource)
{
    return (ScriptObject)Microsoft.JScript.Eval.JScriptEvaluate(JSource, Microsoft.JScript.Vsa.VsaEngine.CreateEngine());
}

Then try this:

var x = Compile("var x = { foo: 'baa', bar: { 'nisse': 'kalle' } };");

var foo = x["foo"];
var bar = (ScriptObject)x["bar"];
var nisse = bar["nisse"];
Sign up to request clarification or add additional context in comments.

1 Comment

VsaEngine is deprecated since VS 2005 (but is still available in VS 2017) :D
1

Seems like there are a number of assumptions here. For instance, that you are getting all the values back on one line. You might try setting vvar this way:

string vvar = variables[i].Trim();

This will trim away unwanted newlines and other whitespace.

Can you post the output of compiler.ToString() for your second example? I would guess that the problem lies in the parsing, not in the JScript compiler.

Also, what happens in cases like this:

var foo = 'a';
foo = 'b';
foo = 'c';

What value does foo end up with?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.