简体   繁体   English

GWT在新选项卡中打开页面

[英]GWT open page in a new tab

I am developing GWT application and I use 我正在开发GWT应用程序,我使用

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

to open new page. 打开新页面。 And it opens in a new tab when called, for example, directly after button click. 并且在调用时会打开一个新选项卡,例如,在按钮单击后直接调用。 But I decided to do some validations on server before opening new page and placed the call to the mentioned above method to the 但我决定在打开新页面之前在服务器上进行一些验证,并将调用上面提到的方法放到

public void onSuccess(Object response) {
}

And it starts to open pages in new window instead of new tab (this is true only for Chrome, other browsers still open it in a new tab). 它开始在新窗口中打开页面而不是新标签(这仅适用于Chrome,其他浏览器仍然在新标签中打开它)。

Can anybody help me? 有谁能够帮我?


I built a small example to illustrate the issue: 我建立了一个小例子来说明问题:

    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) opens both pages in new tabs. Firefox(3.6.8)在新选项卡中打开两个页面。
  • Chrome(6.0) opens "google.com" in new tab and "bing.com" in new window Chrome(6.0)在新标签页中打开“google.com”,在新窗口中打开“bing.com”
  • Opera(10.10) opens in new tabs. Opera(10.10)在新标签中打开。
  • IE(8.0) opens both in new Windows. IE(8.0)在新Windows中打开。

I marked igorbel 's answer as the only correct cos I haven't found any proper way to specify the same behaviour in all situations. 我把igorbel的答案标记为唯一正确的cos我没有找到任何适当的方法在所有情况下指定相同的行为。


I used this code and it works for me in google chrome and mozilla firefox 3.6.8 browsers If you want to open a page in new window you should write code as 我使用此代码,它适用于我在谷歌浏览器和Mozilla Firefox 3.6.8浏览器如果你想在新窗口中打开一个页面,你应该编写代码为

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

If you want to open a page in new tab you should write code as 如果要在新选项卡中打开页面,则应将代码编写为

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

I am not sure you are going to be able to control this the way you want. 我不确定你是否能够按照你想要的方式控制它。 The problem is that browsers can decide when to open windows and when to open tabs. 问题是浏览器可以决定何时打开窗口以及何时打开选项卡。 For example, firefox has the option: "Open new windows in new tabs instead". 例如,firefox有选项:“在新标签中打开新窗口”。 And don't forget the browsers that don't support tabs (yes, those do still exist). 并且不要忘记不支持选项卡的浏览器(是的,那些仍然存在)。

Since this is such a problematic aspect of the user experience, my recommendation would be to reconsider your design. 由于这是用户体验的一个问题,我的建议是重新考虑您的设计。 Is it really that important for you application to differentiate between opening a new tab and opening a new window? 对于应用程序来说,区分打开新选项卡和打开新窗口是否真的很重要?

This code works for me: 这段代码适合我:

  1. Before calling the Async method keep a reference to a new window with empty parameters. 在调用Async方法之前,请保持对具有空参数的新窗口的引用。
  2. At onSuccess() method set the URL of the window. 在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;
}-*/;

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

Interesting thing, chrome will open page in new tab in case if you put window.open(...) instruction into the body of the click handler implementation. 有趣的是,如果你将window.open(...)指令放入click处理程序实现的主体中,chrome将在新选项卡中打开页面。

For example: 例如:

Button someButton = new Button("test",
                    new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            Window.open(...);
                        }

                    });

And a page will be opened in the separate window in case if I will include any Async. 如果我要包含任何异步,将在单独的窗口中打开一个页面。 request into the mentioned code: 请求进入上述代码:

Button someButton = new Button("test",
                    new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            someService.doSomething(new AsyncCallback() {
                                void onSuccess(Object o) {
                                    Window.open(...);
                                }
                                ...
                            });

                        }

                    });

The way Chrome looks at it, calling Window.open() is like trying to open a pop-up window in the user's face. Chrome看待它的方式,调用Window.open()就像试图打开用户脸上的弹出窗口一样。 That's frowned upon and will trigger the built-in pop-up blocker. 这是不赞成的,并将触发内置的弹出窗口阻止程序。 Following a link, according to Chrome, should be the result of a user clicking on a good old anchor tag with an href attribute. 根据Chrome,链接后应该是用户点击带有href属性的旧锚标记的结果。 But here lies the answer you're looking for: you can show a link to the user and change the link target on the fly. 但这里找到了您正在寻找的答案:您可以显示指向用户的链接并动态更改链接目标。 That would qualify as a 'proper' link in Chrome's world. 这将成为Chrome世界中“适当”的链接。

This code works for me: 这段代码适合我:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM