5

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

1
  • maybe this solution can be useful Commented Oct 29, 2011 at 8:39

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

4 Comments

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)
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>
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..
After setting the DocumentText, you have to handle the DocumentCompleted event, and do the InvokeScript call(s) in the handler.
8

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.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.