admin管理员组文章数量:1221921
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.
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.
4 Answers
Reset to default 6You 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.
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
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.
本文标签: netParse JavaScript code in CStack Overflow
版权声明:本文标题:.net - Parse JavaScript code in C# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739289223a2156613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论