Injecting JavaScript into the Webbrowser control is an effective technique of the webpage function displayed in the control control. However, the InnerHTML property of the HTMLELEMENT object may cause System.notsupportedException error.
In order to effectively inject JavaScript, different methods need to be used. The following steps outline the solution:
Get the wead element of the webpage in the webbrowser control:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script"); scriptEl.SetAttribute("type", "text/javascript");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");
The above is the detailed content of How to Safely Inject JavaScript into a WebBrowser Control?. For more information, please follow other related articles on the PHP Chinese website!