C# 如何在msHTML中调用脚本
我正在使用axWebBrowser,我需要使一个脚本在列表框的选定项发生更改时工作 在默认webBrowser控件中,有一种方法,如C# 如何在msHTML中调用脚本,c#,.net,webbrowser-control,axwebbrowser,C#,.net,Webbrowser Control,Axwebbrowser,我正在使用axWebBrowser,我需要使一个脚本在列表框的选定项发生更改时工作 在默认webBrowser控件中,有一种方法,如 WebBrowserEx1.Document.InvokeScript("script") 但在axWebBrowser中,我无法使用任何脚本!并且没有关于此控件的文档 有人知道怎么做吗?一个迟来的答案,但希望仍然可以帮助别人。在使用WebBrowser ActiveX控件时,有多种方法可以调用脚本。同样的技术也可通过以下方式用于WinForms版本的WebBr
WebBrowserEx1.Document.InvokeScript("script")
但在axWebBrowser中,我无法使用任何脚本!并且没有关于此控件的文档
有人知道怎么做吗?一个迟来的答案,但希望仍然可以帮助别人。在使用WebBrowser ActiveX控件时,有多种方法可以调用脚本。同样的技术也可通过以下方式用于WinForms版本的WebBrowser控件和WPF版本: JavaScript的eval必须至少有一个标签才能工作,可以如上所示注入
或者,JavaScript引擎可以使用webBrowser.Document.InvokeScriptsetTimer、new[]{window.external.notifyScript,1}或webBrowser.Navigatejavascript:window.external.notifyScript,void0等异步初始化。回答晚了,但希望仍然可以帮助某些人。在使用WebBrowser ActiveX控件时,有多种方法可以调用脚本。同样的技术也可通过以下方式用于WinForms版本的WebBrowser控件和WPF版本: JavaScript的eval必须至少有一个标签才能工作,可以如上所示注入 或者,可以使用webBrowser.Document.InvokeScriptsetTimer、new[]{window.external.notifyScript,1}或webBrowser.Navigatejavascript:window.external.notifyScript,void0等异步初始化JavaScript引擎
void CallScript(SHDocVw.WebBrowser axWebBrowser)
{
//
// Using C# dynamics, which maps to COM's IDispatch::GetIDsOfNames,
// IDispatch::Invoke
//
dynamic htmlDocument = axWebBrowser.Document;
dynamic htmlWindow = htmlDocument.parentWindow;
// make sure the web page has at least one <script> tag for eval to work
htmlDocument.body.appendChild(htmlDocument.createElement("script"));
// can call any DOM window method
htmlWindow.alert("hello from web page!");
// call a global JavaScript function, e.g.:
// <script>function TestFunc(arg) { alert(arg); }</script>
htmlWindow.TestFunc("Hello again!");
// call any JavaScript via "eval"
var result = (bool)htmlWindow.eval("(function() { return confirm('Continue?'); })()");
MessageBox.Show(result.ToString());
//
// Using .NET reflection:
//
object htmlWindowObject = GetProperty(axWebBrowser.Document, "parentWindow");
// call a global JavaScript function
InvokeScript(htmlWindowObject, "TestFunc", "Hello again!");
// call any JavaScript via "eval"
result = (bool)InvokeScript(htmlWindowObject, "eval", "(function() { return confirm('Continue?'); })()");
MessageBox.Show(result.ToString());
}
static object GetProperty(object callee, string property)
{
return callee.GetType().InvokeMember(property,
BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public,
null, callee, new Object[] { });
}
static object InvokeScript(object callee, string method, params object[] args)
{
return callee.GetType().InvokeMember(method,
BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
null, callee, args);
}