What Is The Recommended Way To Call A Javascript Function From C# Using The Winforms Geckofx Control?
Solution 1:
You can do this using Navigate:
browserControl.Navigate("javascript:void(myFunction('Dave','Smith'))");
Note, I find that the code isn't actually run until the application event loop executes. If that's a problem for you, you might be able to follow the Navigate call with
Application.DoEvents();
Make sure you consider the dangers of calling DoEvents explicitly.
Solution 2:
I know about AutoJSContext class so there is no need for passing javascript to Navigate().
string outString = "";
using (Gecko.AutoJSContext java = new Gecko.AutoJSContext(geckoWebBrowser1.JSContext))
{
java.EvaluateScript(@"window.alert('alert')", out outString );
}
Solution 3:
Dear @SturmCoder and @DavidCornelson are right. but it seems that for version 60.0.24.0
geckoWebBrowser1.JSCall()
and
Gecko.AutoJSContext() which accepts geckoWebBrowser1.JSContext
are absolete and instead of geckoWebBrowser1.JSContext you should write geckoWebBrowser1.Window
and for me this codes works :
string result = "";
using (Gecko.AutoJSContext js= new Gecko.AutoJSContext(geckoWebBrowser1.Window))
{
js.EvaluateScript("myFunction('Dave','Smith');", out result);
}
or even if the website has jQuery you can run like this :
string result = "";
using (Gecko.AutoJSContext js= new Gecko.AutoJSContext(geckoWebBrowser1.Window))
{
js.EvaluateScript(@"alert($('#txt_username').val())", out result);
}
Solution 4:
Besides using Navigate
method, you have this another workaround:
var script = geckofx.Document.CreateElement("script");
script.TextContent = js;
geckofx.Document.GetElementsByTagName("head").First().AppendChild(script);
Post a Comment for "What Is The Recommended Way To Call A Javascript Function From C# Using The Winforms Geckofx Control?"