我被要求改造圍繞IE 9控件構建的自定義網頁瀏覽器。其中一個要求是,必須捕獲所有彈出窗口並將其重定向到新選項卡中。客戶不希望出現任何浮動的獨立窗口。如何通過window.close(javascript)關閉窗口時收到通知?
以前的開發人員實現了以下功能來捕獲這些情況並改爲打開新選項卡。
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;
}
當這些打開的窗口中的一個預計能夠從javascript調用window.close時,就會出現問題。
我希望我可以使用此功能
public void WindowClosing(bool isChildWindow, ref bool cancel)
檢測JavaScript調用和關閉標籤。但是這個功能從來沒有被調用。 javascript函數本身似乎工作。該選項卡的內容被清除。我如何檢測這種情況並關閉標籤?
請注意,IE9 active x控件不是一個硬性要求。但請不要建議我僅僅爲了互聯網的純淨性而切換渲染引擎:P。
編輯:
這是我在C#中DWebBrowserEvents2接口的定義。
[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);
}
下面是一個示例頁面 - http://www.javascript-coder.com/files/window-popup/javascript-window -close-example1.html的。 但我實際上並沒有寫任何特定的頁面來使用,我正在編寫瀏覽器本身。 – Nathanael 2012-02-01 18:39:14