如何在WebBrowser控件中注入Javascript?我试过这个:string newScript = textBox1.Text;HtmlElement head = browserCtrl.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = browserCtrl.Document.CreateElement("script");lblStatus.Text = scriptEl.GetType().ToString();
scriptEl.SetAttribute("type", "text/javascript");head.AppendChild(scriptEl);scriptEl.InnerHtml = "function sayHello() { alert('hello') }";scriptEl.InnerHtml和scriptEl.InnerText都会出现错误:System.NotSupportedException: Property is not supported on this type of HtmlElement.
at System.Windows.Forms.HtmlElement.set_InnerHtml(String value)
at SForceApp.Form1.button1_Click(Object sender, EventArgs e) in d:\jsight\installs\SForceApp\SForceApp\Form1.cs:line 31
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)有什么简单的方法可以将脚本注入dom吗?
3 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];HtmlElement scriptEl = webBrowser1.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;element.text = "function sayHello() { alert('hello') }"; head.AppendChild(scriptEl);webBrowser1.Document.InvokeScript("sayHello");
IHTMLScriptElement
哈士奇WWW
TA贡献1799条经验 获得超6个赞
HtmlDocument doc = browser.Document;HtmlElement head = doc.GetElementsByTagName("head")[0];HtmlElement s = doc.CreateElement("script"); s.SetAttribute("text","function sayHello() { alert('hello'); }");head.AppendChild(s);browser.Document.InvokeScript("sayHello");
添加回答
举报
0/150
提交
取消