如何在 WebBrowser 控件中注入 JavaScript?
javascriptobject oriented programmingfront end technology更新于 2024/7/2 15:08:17
要在 WebBrowser 控件中注入 JavaScript,请使用以下步骤 −
- 首先,在 Visual Studio 中创建一个 Windows 窗体应用程序。
- 现在,将 WebBrowser 控件拖到窗体上
- 设置 Url 属性。
- 右键单击项目,选择 添加引用... → COM → 类型库
- 选择"Microsoft HTML Object Library"
添加以下代码以注入 JavaScript。
private void myWebBrowser(object sender, WebBrowserDocumentCompletedEventArgs e){
// head 元素
HtmlElement hElement = weBrowser.Document.GetElementsByTagName("head")[0];
// script 元素
HtmlElement sElement = weBrowser.Document.CreateElement("script");
IHTMLScriptElement val = (IHTMLScriptElement)sElement.DomElement;
element.text = "function sayHello() {
alert('Weclome')
}";
hElement.AppendChild(sElement);
weBrowser.Document.InvokeScript("How you doing?");
}
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

