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 to get the value of the _url variable from my C # code? Perhaps an open source library? I did this using regex, but I was hoping for a more elegant way.

+6
javascript c # parsing
source share
4 answers

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

This sample 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 

What is it.

+5
source share

You can use a javascript parser, but parsing javascript only so that one value is probably excessive.

+2
source share

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

+2
source share

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

+1
source share

All Articles