C# WebBrowser 禁用alert,返回信息。禁用confirm 返回信息。 页面增加js,页面执行js函数

来源:互联网 发布:网站自然优化 编辑:程序博客网 时间:2024/05/18 00:41

 [ComVisible(true)]//这句话很重要,否则无法接收提示数据数据
    public partial class Wzpolicy : Form
      


private void Wzpolicy_Load(object sender, EventArgs e)
        {
            wb.ObjectForScripting = this;//此属性是得到alert关键设置,否则可以禁止弹出,但是无法得到信息
            wb.ScriptErrorsSuppressed = false;//禁用js错误提示
           
        }            


StringBuilder strAlertBuilder = new StringBuilder();

//禁用alert 信息 信息发送到alertMessage

            strAlertBuilder.Append(" var _alert=window.alert;\n");
            strAlertBuilder.Append(" window.alert=function(s){\n");
            strAlertBuilder.Append(" window.external.alertMessage(s);}\n");

//禁用confirm 信息, confirm 返回true。 信息发送到alertMessage
            strAlertBuilder.Append(" var _confirm=window.confirm;\n");
            strAlertBuilder.Append(" window.confirm=function(s){\n");
            strAlertBuilder.Append(" window.external.alertMessage(s);  return  true;}\n");

            

//把js增加到页面

              HtmlElement he = wb.Document.CreateElement("script");
            he.SetAttribute("type","text/javascript");
            he.SetAttribute("text", strAlertBuilder.ToString());

            wb.Document.Body.AppendChild(he);//add element





//接收提示 信息的方法

  public void alertMessage(string s)
        {
            textBox1.Text += s+"\r\n";
        }

0 0
原创粉丝点击