C# WebBrowser 取 window.open 新窗口 url的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
System.Windows.Forms.WebBrowser wb;       //WebBrowser 对象
 
            wb.NewWindow += new CancelEventHandler(wb_NewWindow);
            wb.DocumentCompleted += delegate
            {              
                #region 处理window.open新开窗口的问题
 
                System.Windows.Forms.HtmlElement html = wb.Document.CreateElement("div");
                html.InnerHtml += "<a id=\"popLink\" href=\"\" target=\"_blank\" style=\"display:none;\"></a>";
                wb.Document.Body.AppendChild(html);
 
                string jsHtml = "";
                jsHtml += "window.open=function(url, title, prop)  ";
                jsHtml += "{";
                jsHtml += "obj = document.getElementById('popLink');  ";
                jsHtml += "obj.style.display='block';  ";
                jsHtml += "obj.href=url;  ";
                jsHtml += "obj.focus();  ";
                jsHtml += "obj.click();  ";
                jsHtml += "obj.style.display='none'  ";
                jsHtml += "} ";
                mshtml.IHTMLDocument2 doc = wb.Document.DomDocument as mshtml.IHTMLDocument2;
                mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;
                win.execScript(jsHtml, "javascript");
 
                #endregion
 
            };
 
 
        static void wb_NewWindow(object sender, CancelEventArgs e)
        {
            System.Windows.Forms.WebBrowser web = (System.Windows.Forms.WebBrowser)sender;
            string newUrl = web.StatusText.ToString();
            showNewWinWithUrlInner(null, newUrl, "", 800, 600, true, false);//自己取到新窗口url后自行处理
            e.Cancel = true;
        }

  

posted @   左正  阅读(1223)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2015-09-18 C#创建Windows Service(Windows 服务)基础教程
2015-09-18 c#写windows服务
2015-09-18 怎么样快速学习AngularJS?
2015-09-18 Web API 安全问题
2015-09-18 ASP.NET Web API身份验证和授权
2014-09-18 验证视图状态 MAC 失败的解决办法
2012-09-18 SQLserver分页 高效率
点击右上角即可分享
微信分享提示