Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 741
raki Avatar asked Sep 17 '09 07:09

raki


People also ask

Can I use JavaScript with C?

It is possible to implement a C API in JavaScript! This is the approach used in many of Emscripten's libraries, like SDL1 and OpenGL. You can use it to write your own APIs to call from C/C++.

Can you call JavaScript in CSS?

No, you can't trigger JavaScript from CSS directly. What you can do is use CSS selectors to find the elements you want to watch in this way, and then watch for mouse events.


1 Answers

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()         {             InitializeComponent();              webBrowser1.DocumentText = @"<html><head>                 <script type='text/javascript'>                     function doIt() {                         alert('hello again');                         return 'i did it!';                     }                 </script>                 </head><body>hello!</body></html>";          }          private void button1_Click(object sender, EventArgs e)         {             object y = webBrowser1.Document.InvokeScript("doIt");         }     } 
like image 172
Gideon Avatar answered Oct 09 '22 14:10

Gideon