2010-10-11 106 views
19

GWT打開的頁面中我開發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", ""); 
       } 
      } 
      ); 
     } 
    }); 
  • 火狐(3.6.8)將打開新的標籤頁都。
  • Chrome(6.0)在新標籤頁中打開「google.com」,在新窗口中打開「bing.com」
  • Opera(10.10)在新標籤頁中打開。
  • IE(8.0)在新窗口中打開。

標誌着我igorbel的答案是唯一的正確因爲我還沒有找到指定在所有情況下相同的行爲的任何適當的方式。


+1

這個問題對我學習如何打開一個新標籤很有幫助: com.google.gwt.user.client.Window.open(pageUrl,「_blank」,「」); – cellepo 2014-05-28 23:03:03

回答

3

我不確定你能否按照你想要的方式來控制它。問題在於瀏覽器可以決定何時打開窗口以及何時打開製表符。例如,firefox可以選擇:「改爲在新標籤中打開新窗口」。不要忘記不支持標籤的瀏覽器(是的,這些標籤仍然存在)。

由於這是用戶體驗的一個有問題的方面,我的建議是重新考慮您的設計。對於您的應用程序來說,打開一個新選項卡並打開一個新窗口是否真的非常重要?

0

此代碼的工作對我來說:

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

有趣的事情, 鑲邊將在如果你把window.open情況下在新標籤中打開頁面(...)指令爲點擊處理程序實現的體。

例如:

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

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

        }); 

和頁面將在單獨的窗口的情況下,打開我是否會包括任何異步。要求進入上述代碼:

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

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

         } 

        }); 
20

我用這段代碼,它適用於我在谷歌鉻和Mozilla Firefox 3.6。8個瀏覽器 如果你想在新窗口打開一個頁面,你應該寫代碼

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

如果要打開新的標籤頁,你應該寫代碼

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

你能提供關於第三個參數的更多信息嗎?這是什麼意思?它是如何使用的? – RAS 2011-12-16 12:29:28

+3

@RAS:請參閱https://developer.mozilla.org/en/DOM/window.open – 2012-03-22 15:03:50

3

此代碼工作對我來說:

  1. 在調用異步方法之前,請保留對帶有空參數的新窗口的引用。
  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

1

的方式Chrome的看着它,調用Window.open()就像是試圖打開用戶的面部彈出窗口。這是不滿的,並會觸發內置的彈出窗口阻止程序。按照Chrome的說法,鏈接之後應該是用戶點擊具有href屬性的優質舊錨定標記的結果。但是這裏有你正在尋找的答案:你可以向用戶顯示一個鏈接並且即時改變鏈接目標。這將成爲Chrome世界中的「適當」鏈接。