我试过这个:
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') }";
Run Code Online (Sandbox Code Playgroud)
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 …Run Code Online (Sandbox Code Playgroud) 我有一个.NET 2.0 WebBrowser控件,用于导航一些没有用户交互的页面(不要问......长篇故事).由于此应用程序的用户性质较少,我已将WebBrowser控件的ScriptErrorsSuppressed属性设置为true,VS 2005状态中包含的文档将"隐藏其源自底层ActiveX控件的所有对话框,不只是脚本错误." 但是,MSDN文章没有提到这一点.我已经设法取消了NewWindow事件,这会阻止弹出窗口,因此需要处理.
任何人都有使用其中一个并成功阻止所有对话框,脚本错误等的经验?
编辑
这不是IE的独立实例,而是生成在Windows窗体应用程序上的WebBrowser控件的实例.任何人都有使用此控件或底层AxSHDocVW的经验吗?
再次编辑
对不起,我忘了提这个...我试图阻止一个JavaScript警报(),只需一个OK按钮.也许我可以转换成IHTMLDocument2对象并以这种方式访问脚本,我已经使用过MSHTML了,有人都知道吗?