Impossible to open a Window as Tab in Firefox and Chrome

Hi,

I’m unable to open a Window as a Tab in Firefox and Chrome. And yes: these browsers are configured to open windows as tabs. This works on other websites. The same code does work as expected with Opera and Safari. My code is like the following:

getMainWindow().open(new ExternalResource(myWin.getURL()), "_blank");

I also tried “_new” and a meaningful name as second parameter.

How can I achieve this in Firefox and Chrome?

I tried it with Vaadin 6.7.1 and FF 7.0.1, Chrome 14.0.835.202 m, Opera 11.51 build 1087, Safari 5.1 (7524.50). IE is not (yet) tested, due to problems with DashLayout.

Andreas

Does
this example
open a new browser tab or window for you? Works for me in Firefox 7.0.1 and Chrome 14.0.835.202 at least using Vaadin 6.7.1.

As far as I know, popup prevention in Firefox does not prevent opening new tabs/windows, so it probably isn’t that.

Marko,

when I use

window.open(new ExternalResource(window.getURL()));

I can’t open a window at all. I have to use

getMainWindow().open(new ExternalResource(window.getURL()), "_blank");

Maybe I’m doing something silly that prevents my app from open windows in a way I want them to open. I created a little maven based project that illustrates my problem. Hopefully you can see (and tell me afterwards) where the problem lies.

Thanks in advance

Andreas
11993.zip (4.85 KB)

Hmm. Looks like Vaadin opens windows using the JS window.open() method with a third “options” parameter. This causes the window to open to a popup window, never as a tab, even if that setting is enabled in Firefox preferences.

However, notice that the mentioned code uses Link to open the window, not Button as you did:

// A button to open a new window
Link openNew = new Link("Open New Window",
        new ExternalResource("/book-examples/book/"),
        "_blank", -1, -1, Window.BORDER_DEFAULT);
layout.addComponent(openNew);

Link renders to a regular HTML hyperlink, which is handled entirely by the browser natively, not by using JavaScript. Therefore, it obeys the open-as-tab setting.

Another way is to work around the above mentioned problem with the window.open() method and call the JavaScript directly:

Button button = new Button("Open Popup Window");
button.addListener(new ClickListener() {
    @Override
    public void buttonClick(ClickEvent event) {
        getWindow().executeJavaScript(
            "window.open('http://vaadin.com/', '_blank')");
    }
});

If you open a new window to your Vaadin app, you can use the window.getURL() to get the URL of the new window.

Marko,

your suggestion with opening the window in JavaScript did the trick. I do it now like this:

getMainWindow().executeJavaScript("window.open('" + win.getURL() + "', '_blank')");

With this code I can open a window as tab in all major browsers except Chrome. Maybe I need an extension for Chrome.

Thanks

Andreas

Hi Marko,

is it also possible to open a pdf file in a tab instead of of a separate window? I have code like this:

final StreamResource resource = new StreamResource(new StreamResource.StreamSource()
{
  @Override
  public InputStream getStream()
  {
    return new ByteArrayInputStream(pdfOut.toByteArray());
  }
}, "test.pdf", getApplication());
getWindow().open(resource, "_blank");

I tried it with the executeJavaScript approach, but I get an empty tab in the browser.

Regards

Andreas

That probably depends on how your browser opens PDFs. Normally when using _blank, the browser creates a new window/tab and then launches the PDF viewer as an external application. If the browser opens PDFs embedded (for example by using a browser plugin), then the window/tab wouldn’t stay empty.

Andreas, did you every find a solution to force it to open in a new tab? I am having the same issue.

Thanks

Hi Riley,

yes, I did! I create a new empty Window, put the Resource in the Window and add the Window to the Application. Then I open this Window via Window.executeJavaScript(url). The url is sth like String url = "window.open('" + w.getURL() + "', '_blank')" Please note, that it depends on the browser you’re using, if a window or tab is created. Chrome will i.e. always create a new window, unless you use the plugin “One Window”. But with this plugin installed, you can’t open a separate window anymore…

HTH

Andreas

Hi, this should be fixed in the core framework in both Vaadin 6 and Vaadin 7 since 6.8.6 (released last December). There’s a new Window.open() / Page.open() overload that takes an additional boolean parameter; passing false should open the page as a tab (unless the browser is configured to open everything as new windows).

See ticket
#7842
.

Hi

I want to open a new URL in a new Tab. My problem is, that my app opens it as a pop-up.
What i do:


getApplication().getMainWindow().open(new ExternalResource("www.vaadin.com"), "_blank", false);

There are 3 possibilities to open a new Page. Window, Tab and Pop-up. Afaik if i set the third parameter to “false” it should be open a new window and the browser handles it if the page open as tab or window. And if the parameter is “true” the page should open as pop-up.

But the above mentioned code opens the new page as pop-up. What i’m doing wrong?

Greets

Yildiz, have you found a solution?