GWT Window.ClosingHandler 需要捕获 url

GWT Window.ClosingHandler catch required url

有没有办法只在需要时触发 Window.ClosingHandler? 在我的入口点内,我添加了以下代码,以便弹出窗口告诉客户,如果他单击外部 link 他将从页面中退出。

public static void addWindowClosingHandler(){
    Window.addWindowClosingHandler(new Window.ClosingHandler() {
        public void onWindowClosing(Window.ClosingEvent closingEvent) {
            closingEvent.setMessage("Are you sure you want to proceed?");
        }
    });
}

问题是,即使我尝试从我的页面下载内容,它也会触发。然后我需要了解被调用的页面是在站点外部还是所需页面是内部的。 有没有办法了解哪个是着陆页?

这取决于您尝试下载文件的方式。如果您只是打开一个新的 window 来下载文件,它将帮助您避免触发 window 关闭处理程序。

    Button downloadBtn = new Button("Download File");
    downloadBtn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Window.open("http://urlforfiledownload", "download", "");
        }
    });

最后我找到了另一个合适的解决方案,即使我最终可能会使用@Ajax提供的“_blank”解决方案。

在此 link https://groups.google.com/forum/embed/#!topic/google-web-toolkit/Y9hnPFtySuU 中描述了使用 iframe 下载文档的不同方式。步骤应该是:

1) 在 activity 中包含一个 iframe,您知道它将在其中触发下载操作。

2) 在按下下载按钮时只需将 url 提供给带有空白目标的 iframe。

除了浏览器决定打开框架内的内容而不是下载内容的情况外,这将是一个很好的解决方案。在那种情况下,它会显示为错误。因此,我可能会接受“_blank”解决方案作为最佳解决方案。