最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to call Javascript method from c# winforms? - Stack Overflow

programmeradmin4浏览0评论

I've a JavaScript file and it contains some methods. I want to call those methods from my winform application. Is it possible? If so can you give me a simple example?

I've tried like this

Process.Start("javascript:showuser('User1');return false;");

But it is not recogniging the showuser method. Because my js file is in remote location (ex : .js)

Can you help me to do this

Thank you

I've a JavaScript file and it contains some methods. I want to call those methods from my winform application. Is it possible? If so can you give me a simple example?

I've tried like this

Process.Start("javascript:showuser('User1');return false;");

But it is not recogniging the showuser method. Because my js file is in remote location (ex : http://mysite.com/userprofile.js)

Can you help me to do this

Thank you

Share Improve this question edited Jan 1, 2010 at 10:31 rahul 187k50 gold badges238 silver badges264 bronze badges asked Jan 1, 2010 at 10:25 NaguNagu 5,11414 gold badges53 silver badges70 bronze badges 1
  • maybe this solution can be useful – garik Commented Oct 29, 2011 at 8:39
Add a comment  | 

3 Answers 3

Reset to default 10

You could use a WebBrowser control. Here's a sample post.

webBrowser1.DocumentText = 
    @"<html><head>
      <script type='text/javascript'>
      function testFunction() {
          alert('test');
      }
      </script>
      </head><body></body></html>";
webBrowser1.Document.InvokeScript("testFunction");

You could possibly use a reference to Microsoft.JScript.dll, and something like the Evaluator method from here; but what exactly are you trying to do? If you are wanting to script your winform, I would be tempted to use IronPython. If you want to automate a browser, you might use the WebBrowser control.

for being able to expose COM objects you must set:

[ComVisible(true)]

outside your class (within your namespace)

something like:

namespace webform
{
    [ComVisible(true)]

    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论