admin管理员组文章数量:1315336
The questions says it all. I have everything wired up and know how to send messages from the browser html to c#, but not the other way.
I should be able to do something like:
browserControl.JSCall("myFunction('Dave','Smith');");
...and in the web code:
function myFunction(firstName, lastName) {
$("#mydiv").text(firstName + ' ' + lastName);
}
Thanks - Dave
The questions says it all. I have everything wired up and know how to send messages from the browser html to c#, but not the other way.
I should be able to do something like:
browserControl.JSCall("myFunction('Dave','Smith');");
...and in the web code:
function myFunction(firstName, lastName) {
$("#mydiv").text(firstName + ' ' + lastName);
}
Thanks - Dave
Share Improve this question asked Aug 17, 2012 at 16:20 David CornelsonDavid Cornelson 3912 gold badges3 silver badges16 bronze badges4 Answers
Reset to default 3You 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.
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 );
}
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);
}
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);
本文标签:
版权声明:本文标题:What is the recommended way to call a javascript function from C# using the WinForms GeckoFX control? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741976028a2408126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论