Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:43:32+00:00 2026-05-16T17:43:32+00:00

I have a C# WinForms application that has a WebBrowser control inside of it.

  • 0

I have a C# WinForms application that has a WebBrowser control inside of it. I would like to perform two-way communication between my C# form and the JavaScript within the embedded web browser control.

I know I can invoke a JavaScript function with InvokeScript, but how can I invoke C# code from JavaScript in a Document? I guess it wont be easy due to security, but is it possible, somehow, anyhow?
These JavaScript functions are supposed to be user functions, pretty much like macros, that would tell the WebBrowser exactly what to do with the help of a whole C# library written by myself. And since this is for a web scraper, JavaScript is the perfect language for these macros since it is pretty much made to access elements in an HTML document.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:43:33+00:00Added an answer on May 16, 2026 at 5:43 pm

    What you need to do is set the ObjectForScripting property on the web browser control to an object containing the C# methods you want to call from JavaScript. Then you can access that object from JavaScript using window.external. The only thing to watch out for is that the object has to have the [ComVisibleAttribute(true)] attribute. I’ve used this successfully for several years.

    Here’s a page with documenation and a simple example: http://msdn.microsoft.com/en-us/library/a0746166.aspx

    Here’s the example from the link (I haven’t tried this code):

    using System;
    using System.Windows.Forms;
    using System.Security.Permissions;
    
    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class Form1 : Form
    {
        private WebBrowser webBrowser1 = new WebBrowser();
        private Button button1 = new Button();
    
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    
        public Form1()
        {
            button1.Text = "call script code from client code";
            button1.Dock = DockStyle.Top;
            button1.Click += new EventHandler(button1_Click);
            webBrowser1.Dock = DockStyle.Fill;
            Controls.Add(webBrowser1);
            Controls.Add(button1);
            Load += new EventHandler(Form1_Load);
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.AllowWebBrowserDrop = false;
            webBrowser1.IsWebBrowserContextMenuEnabled = false;
            webBrowser1.WebBrowserShortcutsEnabled = false;
            webBrowser1.ObjectForScripting = this;
            // Uncomment the following line when you are finished debugging.
            //webBrowser1.ScriptErrorsSuppressed = true;
    
            webBrowser1.DocumentText =
                "<html><head><script>" +
                "function test(message) { alert(message); }" +
                "</script></head><body><button " +
                "onclick=\"window.external.Test('called from script code')\">" +
                "call client code from script code</button>" +
                "</body></html>";
        }
    
        public void Test(String message)
        {
            MessageBox.Show(message, "client code");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Document.InvokeScript("test",
                new String[] { "called from client code" });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TreeView control in my WinForms .NET application that has multiple levels
i have a winforms applicatoin that has a lot of implementations of IOrderDataLoader. Other
I have a winforms application that normally is at about 2-4% CPU. We are
I have C# winforms application that needs to start an external exe from time
I have a winforms application that presently ships with a chm file for context-sensitive
I have a winforms application that is doing the following: on each query: Db
I have an winforms application that was built using MVC. The controller is subscribing
I have a desktop (winforms) application that uses a Firebird database as a data
I have a .net 3.5 WinForms application that runs fine on my machine, but
We have a WinForms application written in C# that uses the AxAcroPDFLib.AxAcroPDF component to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.