【问题标题】:GWT open page in a new tabGWT 在新选项卡中打开页面
【发布时间】:2011-04-23 21:06:56
【问题描述】:

我正在开发 GWT 应用程序并使用

com.google.gwt.user.client.Window.open(pageUrl, "_blank", "");

打开新页面。它在调用时会在新选项卡中打开,例如,在单击按钮后直接打开。 但我决定在打开新页面之前在服务器上进行一些验证,并将上述方法的调用置于

public void onSuccess(Object response) {
}

它开始在新窗口而不是新标签页中打开页面(这仅适用于 Chrome,其他浏览器仍然在新标签页中打开它)。

谁能帮帮我?


我建立了一个小例子来说明这个问题:

    button.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            Window.open("http://www.google.com/", "_blank", "");
            MySampleApplicationServiceAsync serviceAsync = GWT.create(MySampleApplicationService.class);
            serviceAsync.getMessage("Hello, Server!", new AsyncCallback() {

                public void onFailure(Throwable caught) {
                    Window.alert("ERROR");
                }

                public void onSuccess(Object result) {
                    Window.open("http://www.bing.com/", "_blank", "");
                }
            }
            );
        }
    });
  • Firefox(3.6.8) 在新标签页中打开两个页面。
  • Chrome(6.0) 在新标签页中打开“google.com”,在新窗口中打开“bing.com”
  • Opera(10.10) 在新标签页中打开。
  • IE(8.0) 在新 Windows 中打开。

我将 igorbel 的答案标记为唯一正确的因为我没有找到任何适当的方法来指定在所有情况下的相同行为。


【问题讨论】:

  • 这个问题对我学习如何打开一个新标签很有帮助:com.google.gwt.user.client.Window.open(pageUrl, "_blank", "");

标签: java javascript gwt google-chrome


【解决方案1】:

Chrome 看待它的方式,调用Window.open() 就像试图在用户面前打开一个弹出窗口。这是不受欢迎的,并且会触发内置的弹出窗口阻止程序。根据 Chrome 的说法,点击链接应该是用户单击具有href 属性的旧锚标记的结果。但是这就是您要寻找的答案:您可以向用户显示链接并即时更改链接目标。在 Chrome 的世界中,这将是一个“正确”的链接。

【讨论】:

    【解决方案2】:

    此代码适用于我:

    1. 在调用 Async 方法之前,请保留对具有空参数的新窗口的引用。
    2. 在 onSuccess() 方法中设置窗口的 URL。

    Button someButton = new Button("test");
    SelectionListener<ButtonEvent> listener = new SelectionListener<ButtonEvent>() 
    {
        public void componentSelected(ButtonEvent ce)
        {
            final JavaScriptObject window = newWindow("", "", "");
    
            someService.doSomething(new AsyncCallback() 
            {
               public void onSuccess(Object o) 
               {
                    setWindowTarget(window, "http://www.google.com/");
               }
            });            
        }
    }
    someButton.addSelectionListener(listener);
    
    
    private static native JavaScriptObject newWindow(String url, String name, String features)/*-{
        var window = $wnd.open(url, name, features);
        return window;
    }-*/;
    
    private static native void setWindowTarget(JavaScriptObject window, String target)/*-{
        window.location = target;
    }-*/;
    

    发现于: http://groups.google.com/group/google-web-toolkit/browse_thread/thread/574b3b828271ba17

    【讨论】:

      【解决方案3】:

      我使用了这段代码,它适用于我在 google chrome 和 mozilla firefox 3.6.8 浏览器中 如果你想在新窗口中打开一个页面,你应该编写代码为

      Window.open("www.google.com","_blank","enabled");
      

      如果你想在新标签页中打开一个页面,你应该编写代码为

      Window.open("www.google.com","_blank","");
      

      【讨论】:

      • 你能提供更多关于第三个参数的信息吗?它是什么意思以及如何使用?
      【解决方案4】:

      有趣的事情, 如果您将 window.open(...) 指令放入单击处理程序实现的主体中,chrome 将在新选项卡中打开页面。

      例如:

      Button someButton = new Button("test",
                          new ClickHandler() {
      
                              public void onClick(ClickEvent event) {
                                  Window.open(...);
                              }
      
                          });
      

      如果我包含任何 Async,将在单独的窗口中打开一个页面。请求到提到的代码:

      Button someButton = new Button("test",
                          new ClickHandler() {
      
                              public void onClick(ClickEvent event) {
                                  someService.doSomething(new AsyncCallback() {
                                      void onSuccess(Object o) {
                                          Window.open(...);
                                      }
                                      ...
                                  });
      
                              }
      
                          });
      

      【讨论】:

        【解决方案5】:

        此代码适用于我:

        public static native String getURL(String url)/*-{
        return $wnd.open(url,
        'target=_blank')
        }-*/;
        

        【讨论】:

          【解决方案6】:

          我不确定你是否能够以你想要的方式控制它。问题是浏览器可以决定何时打开窗口以及何时打开选项卡。例如,Firefox 有一个选项:“改为在新选项卡中打开新窗口”。并且不要忘记不支持标签的浏览器(是的,那些仍然存在)。

          由于这是用户体验中存在问题的一个方面,我的建议是重新考虑您的设计。对您的应用程序来说,区分打开新标签页和打开新窗口真的那么重要吗?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-01-05
            • 2015-11-14
            • 1970-01-01
            • 2016-09-11
            • 2015-07-26
            • 2013-02-13
            • 2020-12-07
            相关资源
            最近更新 更多