Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JavaScript code in C#

I have the following JavaScript code as a string literal:

var $Page = new function()
{
    var _url= 'http://www.some.url.com';

    this.Download = function()
    {
        window.location = _url;
    }
}

Is there a way I could get the value of the _url variable from my C# code? An open source library perhaps? I did this using a Regular Expression, but I was hoping for a more elegant way.

like image 550
Igal Tabachnik Avatar asked Apr 06 '09 08:04

Igal Tabachnik


2 Answers

You should take a look at the open-source Javascript .NET (http://javascriptdotnet.codeplex.com/) on Codeplex.

This sample of code should help you:

Javascript context = new JavascriptContext();
context.Run("var _url= 'http://www.some.url.com';") // You put your javascript in the function run
String url = (String)context.GetParameter("_url"); // You get your url from javascript

That's it.

like image 164
Deacon Frost Avatar answered Sep 20 '22 12:09

Deacon Frost


There is an open-source JavaScript interpreter in C# at http://jint.codeplex.com, if you need more than just getting the value.

This is now moved to GITHUB

like image 34
Sébastien Ros - MSFT Avatar answered Sep 21 '22 12:09

Sébastien Ros - MSFT