Java GWT 在新标签页中打开页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3907531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT open page in a new tab
提问by Zalivaka
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.
- Chrome(6.0) opens "google.com" in new tab and "bing.com" in new window
- Opera(10.10) opens in new tabs.
- IE(8.0) opens both in new Windows.
- Firefox(3.6.8) 在新标签页中打开两个页面。
- Chrome(6.0) 在新标签页中打开“google.com”,在新窗口中打开“bing.com”
- Opera(10.10) 在新标签页中打开。
- 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 我还没有找到任何正确的方法来在所有情况下指定相同的行为。
采纳答案by igorbel
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". And don't forget the browsers that don't support tabs (yes, those do still exist).
我不确定你是否能够按照你想要的方式控制它。问题是浏览器可以决定何时打开窗口以及何时打开选项卡。例如,firefox 有选项:“改为在新标签页中打开新窗口”。并且不要忘记不支持选项卡的浏览器(是的,这些浏览器仍然存在)。
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?
由于这是用户体验的一个有问题的方面,我的建议是重新考虑您的设计。对于您的应用程序来说,区分打开新选项卡和打开新窗口真的那么重要吗?
回答by applecommander
This code works for me:
这段代码对我有用:
public static native String getURL(String url)/*-{
return $wnd.open(url,
'target=_blank')
}-*/;
回答by Max
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(...) 指令放入点击处理程序实现的主体中,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(...);
}
...
});
}
});
回答by Rajesh
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
我使用了这段代码,它在 google chrome 和 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","");
回答by Raz Oz
This code works for me:
这段代码对我有用:
- Before calling the Async method keep a reference to a new window with empty parameters.
- At onSuccess() method set the URL of the window.
- 在调用 Async 方法之前,保留对带有空参数的新窗口的引用。
- 在 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
回答by Lajos Nagy
The way Chrome looks at it, calling Window.open()
is like trying to open a pop-up window in the user's face. 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. 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 看待它的方式,调用Window.open()
就像试图在用户的脸上打开一个弹出窗口。这是不受欢迎的,并且会触发内置的弹出窗口阻止程序。根据 Chrome 的说法,点击链接应该是用户点击带有href
属性的旧锚标签的结果。但这就是您要寻找的答案:您可以向用户显示链接并即时更改链接目标。这将成为 Chrome 世界中的“正确”链接。