none
关于webbrowser control实现主窗口和子窗口之间元素互相访问的问题 RRS feed

  • 问题

  • 1.html
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
            <script type="text/javascript">
                var a;
                window.onload=function()
                {
                  //  a=window.open("aaa.jsp");
                }
                function aaa()
                {
                    //alert('');
                    a.document.getElementById("as").innerHTML="asdasdsad";
                }
            </script>
        </head>
        <body>
            <a href="aaa.jsp" target="_blank" id="as">aaaaaa</a>

        </body>
    </html>

    2.html

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <script type="text/javascript">
              
                 function aaa()
                {
                    //alert('');
                    window.opener.document.getElementById("as").innerHTML="asdasdsad";
                }
            </script>
        <body>
            <h1 onclick="aaa()" id="af">aaa</h1>
        </body>
    </html>
    使用webbrowser跳转以后,当我点击2.html的<h1>标签时脚本提示window.opener为空,使用windows ie能够正常读取和修改1.html<a>标签的元素,请问这个问题怎么解决

    2014年2月23日 5:47

答案

  • Hi,

    I test it without webbrower  control and it works ok. So I suggest that you should be better post the issue to windows form forum http://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms where you can get better answers.

    In addition, you can refer to the code about the way to pass the Window.Opener object from one WebBrowser control to the next:

    public class WebBrowserTest
    {
        private WebBrowser _wParent;
    
        // entry point
        public void Navigate(string url)
        {
            _wParent = new WebBrowser();
            SHDocVw.WebBrowser_V1 v1 = (SHDocVw.WebBrowser_V1)w.ActiveXInstance;
            v1.NewWindow += v1_NewWindow;
        }
    
        private void v1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
        {
            // tell, that we'll open new page by ourselves
            Processed = true;
    
            // open page by ourselves
            WebBrowser w2 = new WebBrowser();
            w2.Navigate(url);
    
            // wait for document will be loaded to do some magic stuff then
            w2.DocumentCompleted += w2_DocumentCompleted;
        }
    
        // sets opener for popup window
        // after document in popup window was loaded
        private void w2_DocumentCompleted(object sender, EventArgs e)
        {
            WebBrowser popup = (WebBrowser)sender;
            SetOpener(_wparent, popup);
        }
    
        // My most favorite method :)
        // Contains exactly that hack, we're talking about
        private void SetOpener(WebBrowser opener, WebBrowser popup)
        {    
            HtmlWindow htmlPopup = popup.Document.Window;
            HtmlWindow htmlOpener = opener.Document.Window;
    
            // let the dark magic begin
    
            // actually, WebBrowser control is .NET wrapper around IE COM interfaces
            // we can get a bit closer to them access by getting reference to 
            // "mshtml.IHTMLWindow2" field via Reflection
            FieldInfo fi = htmlPopup.GetType().GetField("htmlWindow2", BindingGlags.Instance | BindingFlags.NonPublic);
    
            mshtml.IHTMLWindow2 htmlPopup2 = (mshtml.IHTMLWindow2)fi.GetValue(htmlPopup);
            mshtml.IHTMLWindow2 htmlOpener2 = (mshtml.IHTMLWindow2)fi.GetValue(htmlOpener);
    
            // opener is set here
            htmlPopup2.window.opener = htmlOpener2.window.self;
        }
    }

    Source from http://stackoverflow.com/questions/1051358/javascript-window-opener-object-null-on-net-webbrowser-control-new-window

    Hope it can help you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年2月24日 2:13

全部回复

  • Hi,

    I test it without webbrower  control and it works ok. So I suggest that you should be better post the issue to windows form forum http://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms where you can get better answers.

    In addition, you can refer to the code about the way to pass the Window.Opener object from one WebBrowser control to the next:

    public class WebBrowserTest
    {
        private WebBrowser _wParent;
    
        // entry point
        public void Navigate(string url)
        {
            _wParent = new WebBrowser();
            SHDocVw.WebBrowser_V1 v1 = (SHDocVw.WebBrowser_V1)w.ActiveXInstance;
            v1.NewWindow += v1_NewWindow;
        }
    
        private void v1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
        {
            // tell, that we'll open new page by ourselves
            Processed = true;
    
            // open page by ourselves
            WebBrowser w2 = new WebBrowser();
            w2.Navigate(url);
    
            // wait for document will be loaded to do some magic stuff then
            w2.DocumentCompleted += w2_DocumentCompleted;
        }
    
        // sets opener for popup window
        // after document in popup window was loaded
        private void w2_DocumentCompleted(object sender, EventArgs e)
        {
            WebBrowser popup = (WebBrowser)sender;
            SetOpener(_wparent, popup);
        }
    
        // My most favorite method :)
        // Contains exactly that hack, we're talking about
        private void SetOpener(WebBrowser opener, WebBrowser popup)
        {    
            HtmlWindow htmlPopup = popup.Document.Window;
            HtmlWindow htmlOpener = opener.Document.Window;
    
            // let the dark magic begin
    
            // actually, WebBrowser control is .NET wrapper around IE COM interfaces
            // we can get a bit closer to them access by getting reference to 
            // "mshtml.IHTMLWindow2" field via Reflection
            FieldInfo fi = htmlPopup.GetType().GetField("htmlWindow2", BindingGlags.Instance | BindingFlags.NonPublic);
    
            mshtml.IHTMLWindow2 htmlPopup2 = (mshtml.IHTMLWindow2)fi.GetValue(htmlPopup);
            mshtml.IHTMLWindow2 htmlOpener2 = (mshtml.IHTMLWindow2)fi.GetValue(htmlOpener);
    
            // opener is set here
            htmlPopup2.window.opener = htmlOpener2.window.self;
        }
    }

    Source from http://stackoverflow.com/questions/1051358/javascript-window-opener-object-null-on-net-webbrowser-control-new-window

    Hope it can help you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    2014年2月24日 2:13
  • thank u very much
    2014年2月24日 11:10