How can I be notified when a window is closed by window.close (javascript)?

守給你的承諾、 提交于 2020-01-11 10:35:22

问题


I've been asked to retro-fit a custom web-browser built around the IE 9 control. One of the requirements was that all pop-up windows must be caught and redirected into a new tab. The customer didn't want any floating stand-alone windows to appear.

The previous developer implemented the following function to capture these cases and open a new tab instead.

public void NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
    BrowserExtendedNavigatingEventArgs args = new BrowserExtendedNavigatingEventArgs(ppDisp, new Uri(bstrUrl), null, (UrlContext)dwFlags);
    _Browser.OnStartNewWindow(args);
    Cancel = args.Cancel;
    ppDisp = args.AutomationObject;
}

The problem occurs when one of these opened windows expects to be able to call window.close from javascript.

I was hoping I could use this function

public void WindowClosing(bool isChildWindow, ref bool cancel)

to detect the javascript call and close the tab. But this function is never called. The javascript function itself seems to work. The contents of the tab are cleared. How can I detect this scenario and close the tab?

Note that the IE9 active x control is not a hard requirement. But please don't suggest I switch rendering engines solely for the sake of the purity of the internet :P.

EDIT:

Here is my definition of the DWebBrowserEvents2 interface in C#.

    [ComImport, TypeLibType((short)0x1010), InterfaceType((short)2), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")]
    public interface DWebBrowserEvents2
    {
        [DispId(0x111)]
        void NewWindow3(
            [In, Out, MarshalAs(UnmanagedType.IDispatch)] ref object ppDisp, 
            [In, Out] ref bool Cancel,
            [In] uint dwFlags, 
            [In, MarshalAs(UnmanagedType.BStr)] string bstrUrlContext, 
            [In, MarshalAs(UnmanagedType.BStr)] string bstrUrl);

        [DispId(253)]
        void OnQuit();

        [DispId(263)]
        void WindowClosing(
            [In] bool isChildWindow,
            [In, Out] ref bool cancel);

        [DispId(283)]
        void WindowStateChanged(
            [In] uint dwFlags,
            [In] uint dwVAlidFlagsMask);

        [DispId(271)]
        void NavigateError(
            [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
            [In] ref object URL, [In] ref object frame,
            [In] ref object statusCode, [In, Out] ref bool cancel);
    }

回答1:


Found an answer here: http://social.msdn.microsoft.com/Forums/en/winforms/thread/1cda7799-bf65-4c66-a3bf-488656998191 and it worked for me.

In case that link goes away, here's the answer from that page:

// A delegate type for hooking up close notifications.
public delegate void ClosingEventHandler(object sender, EventArgs e);

// We need to extend the basic Web Browser because when a web page calls
// "window.close()" the containing window isn't closed.
public class ExtendedWebBrowser : WebBrowser
{
  // Define constants from winuser.h
  private const int WM_PARENTNOTIFY = 0x210;
  private const int WM_DESTROY = 2;

  public event ClosingEventHandler Closing;

  protected override void WndProc(ref Message m)
  {
    switch (m.Msg)
    {
      case WM_PARENTNOTIFY:
        if (!DesignMode)
        {
          if (m.WParam.ToInt32() == WM_DESTROY)
          {
            Closing(this, EventArgs.Empty);
          }
        }
        DefWndProc(ref m);
        break;
      default:
        base.WndProc(ref m);
        break;
    }
  }
}

//Then your containing class has the following:
private ExtendedWebBrowser browserControl;
this.browserControl.Closing +=new ClosingEventHandler(browserControl_Closing);
private void browserControl_Closing(object sender, EventArgs e)
{
  this.Close();
}


来源:https://stackoverflow.com/questions/9101113/how-can-i-be-notified-when-a-window-is-closed-by-window-close-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!