raki raki - 4 months ago 35
Javascript Question

Calling a Javascript function in the C# webBrowser control

I am using the webBrowser control in C# to load a webpage and need to call a JavaScript function that returns a string value. I got a solution to use the InvokeScript method, and I tried a lot, but everything has failed.


Can you specify what failed?

My sample below consists of a form with a WebBrowser and a Button.

The object called y in the end has the sentence "i did it!". So with me it works.

public partial class Form1 : Form

        public Form1()

            webBrowser1.DocumentText = @"<html><head>
                <script type='text/javascript'>
                    function doIt() {
                        alert('hello again');
                        return 'i did it!';


        private void button1_Click(object sender, EventArgs e)
            object y = webBrowser1.Document.InvokeScript("doIt");