views:

304

answers:

3

Hi, 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

+4  A: 

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");
Darin Dimitrov
Hi thank you for your response. But can you tell me where can I add js file in my application? or how can i refference to the js file (which is in remote location)
Nagu
In my example I used inline script, but you can also reference your js using using the `src` attribute: `<script type='text/javascript' src='http://mysite.com/userprofile.js'></script>`
Darin Dimitrov
Hi.. I'm sorry it is not working for me. I dont know wats the wrong with me. Even that alert msg also not coming..
Nagu
After setting the DocumentText, you have to handle the DocumentCompleted event, and do the InvokeScript call(s) in the handler.
ShdNx
+2  A: 

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.

Marc Gravell
A: 

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();
        }
    }
}
Cahya Dewanta