Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  191] [ 1]  / answers: 1 / hits: 26315  / 14 Years ago, wed, march 2, 2011, 12:00:00

I have read WebBrowser Control from .Net — How to Inject Javascript, Is it possible to call Javascript method from C# winforms and many others. Those examples were returns function value or alert window (synchronous calls). I have to get result from event handler (async call):



<script type=text/javascript>
window.onload = function() {
var o = new M.Build(document.getElementById(ZID));

M.Events.observe(o, o.Events.Success, function() {
// I have to get some value!!
});

M.Events.observe(o, o.Events.Fault, function() {
// I have to get some value!!
});
}
</script>

More From » winforms

 Answers
13

Calling C# from JavaScript



Simply put, you can expose a C# object
to the WebBrowser that the JavaScript
can call directly The WebBrowser
class exposes a property called
ObjectForScripting that can be set by
your application and becomes the
window.external object within
JavaScript. The object must have the
ComVisibleAttribute set true




C#:



 [System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class ScriptInterface
{
public void callMe()
{
… // Do something interesting
}
}

webBrowser1.ObjectForScripting = new ScriptInterface();


Javascript:



window.external.callMe();


Calling JavaScript in a WebBrowser control from C#


[#93505] Monday, February 28, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyterryp

Total Points: 290
Total Questions: 92
Total Answers: 95

Location: Montenegro
Member since Sun, May 7, 2023
1 Year ago
;