Parse JavaScript code in C#

I have the following JavaScript code as a string literal:

var $Page = new function()
    var _url= '';

    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.



You should take a look at the open-source Javascript .NET ( on Codeplex.

This sample of code should help you:

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

That's it.


There is an open-source JavaScript interpreter in C# at, if you need more than just getting the value.


You could execute the javascript function using the DLR and/or MyJScript.


You could use a javascript parser, but parsing javascript for just that one value is probably way overkill.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.