GWT:在弹出窗口阻止程序阻止RPC之后打开窗口

GWT:在弹出窗口阻止程序阻止RPC之后打开窗口

问题描述:

您好我有以下代码:

Hi I have a following code:

    button.addClickHandler( new ClickHandler( ) {
        @Override   
        public void onClick( ClickEvent event ) {    
             Call 1 --> Window.open( publicBookingUrl, "_blank", null );                
            dispatcher.execute( new HasServicesAction(true), 
                    new ActionCallback<SomeResult>( ){       
                @Override 
                public void onSuccess( SomeResult result ) {
             Call 2 --> Window.open( publicBookingUrl, "_blank", null );
                } 
            });  
        }        
    });

在通话1弹出窗口阻止程序不会阻止打开弹出窗口。它成功地在新选项卡或新窗口中打开窗口。然而在Call2中,弹出式窗口拦截器会阻止弹出窗口,所以用户必须明确地启用弹出窗口
我发现了一篇文章,解释了此背后的推理: https://groups.google.com/forum/?fromgroups=#!topic/google-web-toolkit/V0s7goJxuhc
不幸的是,此解决方案对我无效。

In Call 1 popup blocker does not block the popup from opening. It successfully opens a window in a new tab or in new window. In Call2 however popup blocker will prevent popup, so user have to explicitly enable popup. I found a post explaining the reasoning behind this: https://groups.google.com/forum/?fromgroups=#!topic/google-web-toolkit/V0s7goJxuhc Unfortunately this solution doesn't work for me.

有谁知道为什么会出现这种情况?我们如何解决这个问题?

Does anyone know why this is the case? How can we get around this?

预先感谢。

由于您链接到的页面显示,因此只能通过直接用户操作打开窗口。您可以通过在RPC调用之前打开窗口并在RPC调用返回后设置窗口的URL来解决此问题。 GWT内置的 Window 不会公开所有底层的窗口对象的属性,因此自定义实现是必需的:

As the page you linked to indicated, windows may only be opened as a result of a direct user action. You can get around this by opening the window before your RPC call and setting the URL of the window after the RPC call returns. GWT's built-in Window doesn't expose all of the underlying window object's properties, so a custom implementation is necessary:

public class MyWindow extends JavaScriptObject {
  // All types that extend JavaScriptObject must have a protected,
  // no-args constructor. 
  protected MyWindow() {}

  public static native MyWindow open(String url, String target, String options) /*-{
    return $wnd.open(url, target, options);
  }-*/;

  public native void close() /*-{
    this.close();
  }-*/;

  public native void setUrl(String url) /*-{
    if (this.location) {
      this.location = url;
    }
  }-*/;
}

然后在您的点击处理程序中:

Then in your click handler:

public void onClick(ClickEvent event) {
  final MyWindow window = MyWindow.open(null, "_blank", null);

  dispatcher.execute(new HasServicesAction(true), 
      new ActionCallback<SomeResult>( ){       
        @Override 
        public void onSuccess(SomeResult result) {
          if (result.isGood()) {
            window.setUrl(publicBookingUrl);
          } else {
            window.close();
          }
        }
      });  
}

请注意,如果您调用 setUrl() 更改您赢得打开窗口的原点'之后可以修改任何属性或调用任何功能。

Note that, if your call to setUrl() changes the origin of the opened window you won't be able to modify any properties or call any functions afterwards.